我使用此代码更改UISearchBar取消按钮标题:
-(void)searchDisplayControllerWillBeginSearch:(UISearchDisplayController *)controller{
self.searchDisplayController.searchBar.showsCancelButton = YES;
UIView* view=_otsinguRiba.subviews[0];
for (UIView *subView in view.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
UIButton *cancelButton = (UIButton*)subView;
if (cancelButton) {
[cancelButton setTitle:@"Test") forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
}
}
}
}
虽然更改文本可以正常工作,但更改颜色并不适用。它保持黑色。
答案 0 :(得分:10)
我在一段时间后找到了答案,但我不记得在哪里。这是我用来设置文字颜色的代码。
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor],NSForegroundColorAttributeName,
//[UIColor whiteColor],UITextAttributeTextShadowColor,
//[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],UITextAttributeTextShadowOffset,
nil]
forState:UIControlStateNormal];
答案 1 :(得分:0)
您可以利用iOS运行时属性_cancelButton
来实现此目的。
UIButton *cancelButton = [self.searchDisplayController.searchBar valueForKey:@"_cancelButton"];
[cancelButton setTitleColor:[UIColor yourColor] forState:UIControlStateNormal];