通過 [searchBarsetShowsCancelButton:YES];設置之後默認是“cancel”,想改為中文的
在stackoverflow上參考了很多方法都沒用
這裡的關鍵是要獲取到 searchbar中得 cancelButton
通過打印 [searchbar subviews]
得到結果如下
(lldb) po [searchBar subviews]
<__NSArrayM 0xd6b0db0>(
+H; layer = ) 也就是其只有一個subview,在stackoverflow上得到些其實 這個cancelbutton在更深一層 打印 [[[searchBar subviews] objectAtIndex:0] subviews] 得到結果:
(lldb) po [[[searchBar subviews] objectAtIndex:0] subviews] <__NSArrayM 0xd6b0dd0>( ) 也就是這個UINavigationButton,對它進行設置 代碼如下
searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 240, 24)];
//導航條的搜索條
searchBar.delegate = self;
[searchBar setShowsCancelButton:YES];
[searchBar setTintColor:[UIColor blackColor]];
[searchBar setBarTintColor:[UIColor clearColor]];
[searchBar setPlaceholder:@"搜索劇集"];
for(UIView *view in [[[searchBar subviews] objectAtIndex:0] subviews]) {
if([view isKindOfClass:[NSClassFromString(@"UINavigationButton") class]]) {
UIButton * cancel =(UIButton *)view;
[cancel setTitle:@"取消" forState:UIControlStateNormal];
[cancel setTintColor:[UIColor blackColor]];
[cancel.titleLabel setTextColor:[UIColor blackColor]];
}
}