做项目经常会用到搜索功能,搜索的取消按钮一直是“cancel”,我们把它变为“取消”
在网上搜索出来的一般是这样
UISearchBar *searchbar ;
UIView* textView = searchbar.subviews[0];
for (UIView * view in textView.subviews){
if([view isKindofClass:[UIButton calss]]){
[view setTitle:@"取消"forState:UIControlStateNormal];
}
}
但是我发现这样并不管用,于是我打印了一下textView 出来以下结果
textView (
"<UISearchBarBackground: 0x9423f70; frame = (0 0; 200 44); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x94240c0>>",
"<UISearchBarTextField: 0x9424590; frame = (0 0; 0 0); text = ''; clipsToBounds = YES; opaque = NO; gestureRecognizers = <NSArray: 0x942a270>; layer = <CALayer: 0x94247a0>>",
"<UINavigationButton: 0x9428ad0; frame = (0 0; 54 30); opaque = NO; layer = <CALayer: 0x9428c80>>"
)
可以看到呢这上面并没有UIButton 的对象,只有一个UINavigationButton的一个对象,显然这个就是Cancelbutton了,于是把上面的代码改成这样就一切大功告成了
UIButton *cancelButton;
UIView *textView=SearchbarText.subviews[0];
for (UIView * view in topView.subviews) {
if ([view isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
cancelButton = (UIButton*)view;
}
}
if (cancelButton) {
[cancelButton setTitle:@"取消" forState:UIControlStateNormal];
}
-----------------------------------------------------我是一条分割线--------------------------------------------------------------------
其实呢上面的方法,并不太好,虽然这样做并没有什么问题。但是前段时间做国际化的时候突然意识到,之所以搜索框在系统语言为中文的情况下还是cancel是因为
我们创建工程时,默认的语言其实是英文,但是我们给这些加的都是中文,所以一般不会有什么问题,只是遇到serachbar 的时候cancel 怎么也变不回中文。
知道了这些解决也非常简单了
解决方法:
在 PROJECT->info ->Localizations 里添加Chinese(Simplified)就好了,它自己就变成了取消了。