如何更改UISearchbar取消button的文本颜色

我有一个UISearchBar,如下所示。 如何更改取消button的文字颜色?

在这里输入图像说明

这个问题刚才被问到了,所以我想这个问题的人已经find了解决办法。 但是,以防万一碰巧遇到同样的问题。 这是我的解决scheme。

我有一个UISearchBar与一个取消button,只有当UISearchBar的文本字段被点击时出现。 在UISearchBar的子类中覆盖 – (void)layoutSubviews的解决scheme因此不是我的select。 无论如何,我做了一个UISearchBar(CustomSearchBar)的子类与公共方法设置取消button的字体和textColor。 当我创buildUISearchBar时,我确保search栏的文本字段委托被设置为self,并且创buildsearch栏的类实现了UITextFieldDelegate协议。 当用户点击search栏的文本字段时,会通知其委托并调用CustomSearchBar的方法。 我之所以这样做,是因为这是取消button出现的时刻,因此我知道它在视图层次结构上,我可以进行自定义。

代码如下:

用于在MyRootViewController中创buildUISearchBar

CustomSearchBar *searchBar = [[CustomSearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 40)]; [searchBar setBarStyle:UIBarStyleDefault]; [searchBar setTintColor:[UIColor whiteColor]]; for (UIView *view in [searchBar subviews]) { if ([view isKindOfClass:[UITextField class]]) { UITextField *searchTextField = (UITextField *)view; [searchTextField setDelegate:self]; } } self.searchBar = searchBar; [searchBar release]; 

在MyRootViewController中的UITextFieldDelegate(确保它实现了UITextFieldDelegate协议)

 - (void)textFieldDidBeginEditing:(UITextField *)textField { [self.searchBar setCloseButtonFont:[UIFont fontWithName:@"American Typewriter" size:14] textColor:[UIColor grayColor]]; } 

这是UISearchBar的子类中的公共方法

 - (void)setCloseButtonFont:(UIFont *)font textColor:(UIColor *)textColor { UIButton *cancelButton = nil; for(UIView *subView in self.subviews) { if([subView isKindOfClass:[UIButton class]]) { cancelButton = (UIButton*)subView; } } if (cancelButton) { /* For some strange reason, this code changes the font but not the text color. I assume some other internal customizations make this not possible: UILabel *titleLabel = [cancelButton titleLabel]; [titleLabel setFont:font]; [titleLabel setTextColor:[UIColor redColor]];*/ // Therefore I had to create view with a label on top: UIView *overlay = [[UIView alloc] initWithFrame:CGRectMake(2, 2, kCancelButtonWidth, kCancelButtonLabelHeight)]; [overlay setBackgroundColor:[UIColor whiteColor]]; [overlay setUserInteractionEnabled:NO]; // This is important for the cancel button to work [cancelButton addSubview:overlay]; UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 2, kCancelButtonWidth, kCancelButtonLabelHeight)]; [newLabel setFont:font]; [newLabel setTextColor:textColor]; // Text "Cancel" should be localized for other languages [newLabel setText:@"Cancel"]; [newLabel setTextAlignment:UITextAlignmentCenter]; // This is important for the cancel button to work [newLabel setUserInteractionEnabled:NO]; [overlay addSubview:newLabel]; [newLabel release]; [overlay release]; } } 

而不是做所有这些奇特的事情只是像这样实现SearchBarTextDidBeginEditing

 - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar { // only show the status bar's cancel button while in edit mode sbar (UISearchBar) searchBar.showsCancelButton = YES; searchBar.autocorrectionType = UITextAutocorrectionTypeNo; UIColor *desiredColor = [UIColor colorWithRed:212.0/255.0 green:237.0/255.0 blue:187.0/255.0 alpha:1.0]; for (UIView *subView in searchBar.subviews){ if([subView isKindOfClass:[UIButton class]]){ NSLog(@"this is button type"); [(UIButton *)subView setTintColor:desiredColor]; [(UIButton *)subView setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; } } 

Gyanerdra的回答很好。 但是对于iOS7,我需要进行以下更改才能在我的应用程序中工作。

 NSArray *childViews; if ( (APP).isIOS7 ) { childViews = [[searchBar.subviews objectAtIndex:0] subviews]; } else { childViews =searchBar.subviews; } for (UIView *subView in childViews ) { if([subView isKindOfClass:[UIButton class]]){ [(UIButton *)subView setTintColor:desiredColor]; [(UIButton *)subView setTitleColor:desiredColor forState:UIControlStateNormal]; } } 

看来,对于iOS7的search栏被包含在父视图。 希望这有助于某人。 b

你可以inheritanceUISearchBar并编写你自己的- (void)layoutSubviews方法。 在这个方法中,循环通过它的子视图并获取cancelButton。 其余的应该是直接的。

您可以利用iOS运行时属性_cancelButton来实现此目的。

 UIButton *cancelButton = [searchBar valueForKey:@"_cancelButton"]; [cancelButton setTitleColor:[UIColor yourColor] forState:UIControlStateNormal]; 

更改文字后无法更改UISearchBar取消button的标题颜色。

KVC

UIButton * button = [_searchBar valueForKey:@“_ cancelButton”]; button.titleLabel.font = [UIFont systemFontOfSize:13];