【发布时间】:2011-08-09 07:41:50
【问题描述】:
我有一个 UISearchBar,如下所示。如何更改取消按钮的文本颜色?
【问题讨论】:
标签: iphone objective-c ios uisearchbar
我有一个 UISearchBar,如下所示。如何更改取消按钮的文本颜色?
【问题讨论】:
标签: iphone objective-c ios uisearchbar
这个问题是不久前提出的,因此我假设提出问题的人已经找到了解决方案。但以防万一其他人碰巧遇到同样的问题。这是我的解决方案。
我有一个带有取消按钮的 UISearchBar,该按钮仅在点击 UISearchBar 的文本字段时出现。因此,在 UISearchBar 的子类中覆盖 -(void)layoutSubviews 的解决方案不是我的选择。无论如何,我做了一个 UISearchBar (CustomSearchBar) 的子类,它使用一个公共方法来设置取消按钮的字体和 textColor。当我创建 UISearchBar 时,我确保搜索栏的文本字段委托设置为 self 并且创建搜索栏的类实现 UITextFieldDelegate 协议。当用户点击搜索栏的文本字段时,它的代理会被通知并调用 CustomSearchBar 的方法。我在这里这样做的原因是因为它是取消按钮出现的那一刻,因此我知道它在视图层次结构中,我可以对其进行自定义。
代码如下:
用于在 MyRootViewController 中创建 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。其余的应该是直截了当的。
【讨论】:
您可以利用 iOS 运行时属性 _cancelButton 来实现此目的。
UIButton *cancelButton = [searchBar valueForKey:@"_cancelButton"];
[cancelButton setTitleColor:[UIColor yourColor] forState:UIControlStateNormal];
Unable to change UISearchBar cancel button title color after changing it's text.
【讨论】:
KVC
UIButton *button = [_searchBar valueForKey:@"_cancelButton"]; button.titleLabel.font = [UIFont systemFontOfSize:13];
【讨论】: