1

我有以下代码可以更改 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

4

3 回答 3

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;
于 2013-10-03T16:24:01.933 回答
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
}
于 2013-10-18T15:32:07.580 回答
0

您可以利用 iOS 运行时属性_cancelButton来实现这一点。

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

用于更改文本

[self.searchBar setValue:@"customString" forKey:@"_cancelButtonText"];
于 2015-11-26T10:38:34.953 回答