the first method:
[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]].title = @"取消";
The second method:
[self.searchBar setValue:@"取消" forKey:@"_cancelButtonText"];
The third method:
Comply with UISearchBarDelegate proxy protocol;
then implement-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar this proxy method
/**
* 开始编辑的时候
*/
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
searchBar.showsCancelButton = YES;//必须设置
for (id cencelButton in [searchBar.subviews[0] subviews]){
if([cencelButton isKindOfClass:[UIButton class]]) {
UIButton *btn = (UIButton *)cencelButton;
[btn setTitle:@"取消" forState:UIControlStateNormal];
}
}
}
searchBar.showsCancelButton = NO;
How to not set to YES the first time it will still display English
searchBar.showsCancelButton = YES;//YES must be set
Set to YES and it will be Chinese from the beginning