我有一个带有取消按钮的 UISearchBar(使用 -(void)setShowsCancelButton:animated
显示)。我已经像这样更改了搜索栏的 tintColor
以尝试获得一个灰色的搜索栏:
UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];
searchBar.tintColor = [UIColor colorWithWhite:0.8 alpha:1.0];
这是现在的样子 - 注意取消按钮也是灰色的:http://twitpic.com/c0hte
有没有办法单独设置取消按钮的颜色,使其看起来更像这样:http://twitpic.com/c0i6q
最佳答案
您可以使用 UIAppearance
设置取消按钮的样式,而无需迭代 UISearchBar
的 subview ,但是 the UIButton
header does not currently have any methods annotated with UI_APPEARANCE_SELECTOR
.
编辑:向下钻取 subview 直到获得取消按钮
但这通常返回 nil 直到
searchBar.setShowsCancelButton(true, animated: true)
被调用。
extension UISearchBar {
var cancelButton : UIButton? {
if let view = self.subviews.first {
for subView in view.subviews {
if let cancelButton = subView as? UIButton {
return cancelButton
}
}
}
return nil
}
}
关于ios - 在 UISearchBar 中设置取消按钮的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1200149/