ios - 在 UISearchBar 中设置取消按钮的样式

标签 ios objective-c iphone cocoa-touch uisearchbar

我有一个带有取消按钮的 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/

相关文章:

ios - Darwin /引用手册页丢失

iphone - 使用 CoreText 显示 NSAttributedString

ios - 将拍摄的图像分配给 ImageView

iphone - 编辑 : UITextView Label is Cut in Half (Horizontally)

iphone - Objective-C Bonjour/TCP 堆栈

php - 使用 MySQL 结果更改 UITextField(xcode 项目)

iphone - 核心数据: Subquery Performance Problem

iphone - 在 Google Analytics 报告中,访问次数即将到来,但没有 PageViews 的数据

iphone - 更改 MGTwitterEngine 的委托(delegate)

javascript - 错误 : No Firebase App '[DEFAULT]' has been created - call Firebase App. 初始化应用程序()