[英]How do I change the UISearchbar cancel button's text color
I have a UISearchBar as seen below. 我有一个UISearchBar,如下所示。 How can I change the text color for the cancel button? 如何更改取消按钮的文本颜色?
This question was asked a while ago, therefore I assume that the person who asked has already found a solution. 刚才提出这个问题,因此我认为提问的人已经找到了解决方案。 But just in case some others happen to bump into the same problem. 但是为了防止其他一些碰巧遇到同样的问题。 Here is my solution. 这是我的解决方案。
I have a UISearchBar with a cancel button that appears only when the textfield of the UISearchBar is tapped. 我有一个带有取消按钮的UISearchBar,只有在点击UISearchBar的文本字段时才会出现。 The solution of overriding -(void)layoutSubviews in a subclass of UISearchBar was therefore not an option for me. 因此,在UISearchBar的子类中覆盖 - (void)layoutSubviews的解决方案对我来说不是一个选项。 Anyway I did a subclass of UISearchBar (CustomSearchBar) with a public method for setting the font and textColor of the cancel button. 无论如何,我做了一个UISearchBar(CustomSearchBar)的子类,使用了一个公共方法来设置取消按钮的字体和textColor。 When I create the UISearchBar I make sure that the search bar's textfield delegate is set to self and the class that creates the search bar, implements the UITextFieldDelegate protocol. 当我创建UISearchBar时,我确保搜索栏的textfield委托设置为self,并且创建搜索栏的类实现UITextFieldDelegate协议。 When the user taps on the search bar's textfield, its delegate is informed and calls the method of CustomSearchBar. 当用户点击搜索栏的文本字段时,会通知其委托并调用CustomSearchBar的方法。 The reason why I do it here, is because it's the moment when the cancel button appears and therefore I know that it is on the view hierarchy and I can do its customization. 我在这里做的原因是因为它是取消按钮出现的时刻,因此我知道它在视图层次结构上,我可以进行自定义。
Here's the code: 这是代码:
For creating the UISearchBar in MyRootViewController 用于在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];
UITextFieldDelegate in MyRootViewController (make sure that it implements the UITextFieldDelegate protocol) MyRootViewController中的UITextFieldDelegate(确保它实现了UITextFieldDelegate协议)
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self.searchBar setCloseButtonFont:[UIFont fontWithName:@"American Typewriter" size:14] textColor:[UIColor grayColor]];
}
And this is the public method in the UISearchBar's subclass 这是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];
}
}
Instead of doing all these fancy things just implement the SearchBarTextDidBeginEditing like this 而不是做所有这些花哨的事情只是像这样实现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's answer works well. Gyanerdra的答案很有效。 But for iOS7 I needed to make the following change for it work in my app. 但对于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];
}
}
It seems that for iOS7 the search bar is enclosed in a parent view. 似乎对于iOS7,搜索栏包含在父视图中。 Hope this helps someone. 希望这有助于某人。 b b
You can subclass UISearchBar and write your own - (void)layoutSubviews
method. 您可以继承UISearchBar并编写自己的- (void)layoutSubviews
方法。 In this method, loop through its subviews and get the cancelButton. 在此方法中,循环遍历其子视图并获取cancelButton。 The rest should be straight forward. 其余的应该是直截了当的。
You can take advantage of the iOS Runtime Property _cancelButton
to achieve this. 您可以利用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. 更改文本后,无法更改UISearchBar取消按钮标题颜色。
KVC KVC
UIButton *button = [_searchBar valueForKey:@"_cancelButton"]; UIButton * button = [_ searchBar valueForKey:@“_ cancelButton”]; button.titleLabel.font = [UIFont systemFontOfSize:13]; button.titleLabel.font = [UIFont systemFontOfSize:13];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.