在iOS 7中更改取消按钮背景颜色和UISearchBar文本

时间:2013-10-03 15:55:39

标签: iphone objective-c cocoa-touch ios7 uisearchbar

我有以下代码更改UISearchBar中“取消”按钮的背景。

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

3 个答案:

答案 0 :(得分:6)

<强> [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中更改了searchBar子视图层次结构,请尝试以下操作:

in iOS7:

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


iOS6 and before:

NSArray *searchBarSubViews =  self.searchBar.subviews;

答案 1 :(得分:1)

解决方案:我创建了自己的按钮。通过这种方式,我可以管理所有属性,而无需了解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
}

答案 2 :(得分:0)

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

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

用于更改文字

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