我有一个如下所示的UISearchBar。我该如何更改取消按钮的文本颜色?
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中,搜索栏被包含在一个父视图中。 希望这能帮助到某些人。 b
您可以创建 UISearchBar 的子类,并编写自己的 - (void)layoutSubviews
方法。在此方法中,循环遍历其子视图并获取 cancelButton。其余部分应该是直截了当的。
_cancelButton
来实现这个功能。UIButton *cancelButton = [searchBar valueForKey:@"_cancelButton"];
[cancelButton setTitleColor:[UIColor yourColor] forState:UIControlStateNormal];
KVC
UIButton *button = [_searchBar valueForKey:@"_cancelButton"]; button.titleLabel.font = [UIFont systemFontOfSize:13];
KVC
UIButton *button = [_searchBar valueForKey:@"_cancelButton"]; button.titleLabel.font = [UIFont systemFontOfSize:13];