更改取消按钮BackgroundColor和UISearchBar在iOS7中的文本

问题描述:

我有以下代码可以更改UISearchBar中“取消”按钮的背景。更改取消按钮BackgroundColor和UISearchBar在iOS7中的文本

for(UIView *subview in self.searchBar.subviews){ 
    if ([subview isKindOfClass:[UIButton class]]) { 
     [(UIButton *)subview setEnabled:YES]; 
     [(UIButton *)subview setTitle:@"New Button Text" forState:UIControlStateNormal]; 
     ((UIButton *)subview).tintColor = [UIColor colorWithRed:4/255.0 green:119/255.0 blue:152/255.0 alpha:1.0]; 
    } 
} 

问题是这段代码在iOS7中不起作用! UISearchBar中的视图结构发生了什么变化?

编辑:这里测试,这是的UISearchBar的新层次:

UISearchBar: 
---UIView: 
------UISearchBarBackground 
------UISearchBarTextField 
------UINavigationButton 

的问题是,我无法测试if ([sub isKindOfClass:[UINavigationButton class]])。这条线抛出一个编译错误:Use of undeclared identifier: UINavigationButton

解决方案:我创造了我自己的按钮。这样我就可以管理所有属性,而无需了解Apple对元素所做的操作

我只需创建UIView aux即可包含searchBar和新Button。

self.searchBar.showsCancelButton = NO; //don`t show the original cancelButton 

self.cancelSearchButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
self.cancelSearchButton.frame = CGRectMake(250, 6, 60, 31); 
[self.cancelSearchButton setTitle:@"Cancelar" forState:UIControlStateNormal]; 
[self.cancelSearchButton addTarget:self action:@selector(searchBarCancelButtonClicked) forControlEvents:UIControlEventTouchUpInside]; 
NSString *fontName = [[self.cancelSearchButton titleLabel] font].fontName; 
[[self.cancelSearchButton titleLabel] setFont:[UIFont fontWithName:fontName size:11.0]]; 
UIView *aux = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
aux.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0 blue:230/255.0 alpha:1.0]; 
aux.layer.borderWidth = 1.0f; 
aux.layer.borderColor = [UIColor lightGrayColor].CGColor; 
aux.layer.cornerRadius = 0.0f; 
[aux addSubview:self.searchBar]; 
[aux addSubview:self.cancelSearchButton]; 

- (void)searchBarCancelButtonClicked{ 
    //do whatever I want to do here 
} 
+0

这个方法的问题是默认情况下你不会得到漂亮的默认动画...或者如果你能我想知道如何。 – x89a10

[Edited]

试试这个代码。

加入AppDelegate如果你想改变所有取消按钮。

[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                             [UIColor redColor], 
                             UITextAttributeTextColor, 
                             [UIColor whiteColor], 
                             UITextAttributeTextShadowColor, 
                             [NSValue valueWithUIOffset:UIOffsetMake(0, 0)], 
                             UITextAttributeTextShadowOffset, 
                             nil] 
                          forState:UIControlStateNormal]; 

在iOS7,我们需要添加objectAtIndex:0 and subviews.

搜索栏子视图层次结构中iOS7被改变,尝试以下:

in iOS7: 

NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews]; 


iOS6 and before: 

NSArray *searchBarSubViews = self.searchBar.subviews; 
+1

很抱歉,但它没有工作。 –

+0

我编辑了我的答案,请重新检查它。它在我的项目中工作正常。 –

+0

这也不适合我。 @Yahiko,这是从iOS 7.0.3仍然工作?迭代子视图时,我只看到2个元素;没有UIBarButtonItems或UIButtons。困惑。 – Daniel

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

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

对于更改文本

[self.searchBar setValue:@"customString" forKey:@"_cancelButtonText"];