UISearchBar取消按钮颜色?

时间:2010-05-07 09:22:59

标签: ios cocoa-touch uisearchbar background-color uibarbuttonitem

当我将UISearchBar放入Interface Builder中的视图中,并将其样式更改为Black Opaque时,取消按钮保持不合适的蓝色/灰色并且不会变黑。

如何将取消按钮设为黑色?

编辑:它确实像这样工作:

// Assume a UISearchBar searchBar.
NSArray *subviews = [searchBar subviews];

// The index depends on how you configure the searchBar.
UIButton *cancelButton = [subviews objectAtIndex:3];

// Set the style to "normal" style.
[cancelButton setStyle:0];

setStyle:方法来自私有框架,因此在将应用程序提交给Apple时可能会出现问题。

10 个答案:

答案 0 :(得分:111)

我使用了这样的东西并与我合作:

[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor blackColor]];

它将取消按钮颜色更改为黑色。

对于iOS 9.0的

更新,方法appearanceWhenContainedIn已弃用,请改用appearanceWhenContainedInInstancesOfClasses

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:[UIColor blackColor]];

在Swift 3中:

UIBarButtonItem.appearance(whenContainedInInstancesOf:[UISearchBar.self]).tintColor = UIColor.black

答案 1 :(得分:32)

您的解决方案的问题是代码假设objectAtIndex:3是取消按钮。这不仅会生成编译器警告,而且如果您以编程方式显示“取消”按钮(例如使用[searchBar setShowsCancelButton:YES],则可能会导致应用程序崩溃。

更简单的解决方案是使用以下方法在ViewDidLoad()中设置整个搜索栏的样式:

searchBar.tintColor = [UIColor colorWithWhite:0.3 alpha:1.0];

这会覆盖界面生成器中设置的样式但是也会将“取消”按钮的颜色更改为与整个栏相同的颜色(尽管它不允许您单独设置“取消”按钮的样式,不幸的是。

答案 2 :(得分:5)

试试这个并看看:(我在下面的代码中测试了Swift 4.1 - Xcode 9.3-beta4

@IBOutlet weak var sbSearchBar: UISearchBar!

sbSearchBar.tintColor = UIColor.red
sbSearchBar.showsCancelButton = true
sbSearchBar.barTintColor = UIColor.blue
sbSearchBar.tintColor = UIColor.red

if let buttonItem = sbSearchBar.subviews.first?.subviews.last as? UIButton {
    buttonItem.setTitleColor(UIColor.yellow, for: .normal)
}

enter image description here

答案 3 :(得分:4)

这是Swift 3的Hossam Ghareeb's answer above的更新版本:

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self] ).tintColor = UIColor.red

但是如果它已经在UIBarButtonItem的其他位置设置了,那么这不会覆盖外观。

例如,在我的导航栏控制器中,我不得不改变它:

UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.white], for: UIControlState.normal)

为了解决上述解决方案的问题:

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self] ).setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.white], for: UIControlState.normal)

答案 4 :(得分:3)

在Swift 4.2中

let appearance = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])
appearance.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor(named: "goldColor")!], for: .normal)

这对我有用。谢谢@Tim Semple

答案 5 :(得分:1)

我已经采取了 Benjamin的答案,并将其与安全Array查询相结合,以生成一个简短的安全功能版本:

searchController.searchBar.tintColor = UIColor.whiteColor()
(searchController.searchBar.subviews[safe: 0]?.subviews as? [UIView])?
    .filter({$0.isKindOfClass(UITextField)})
    .map({$0.tintColor = .lightGrayColor()})

这会导致“取消”按钮变白并在键入灰色时将光标着色。否则它将是白色的,因此没有看到。 searchControllerUISearchController类型的对象。如果有人想在结果控制器中使用它,请将其替换为self

safe:下标的实施是 nkukushkin's 回答:

extension Array {
    subscript(safe index: Int) -> T? {
        return indices(self) ~= index ? self[index] : nil
    }
}

答案 6 :(得分:1)

点击搜索栏,在Interface Builder的视图下设置色调颜色。

enter image description here

答案 7 :(得分:1)

let view: UIView = self.searchBar.subviews[0] as UIView
let subViewsArray = view.subviews

for subView: UIView in subViewsArray {
    if let cancelButt = subView as? UIButton{
        cancelButt.setTitleColor(UIColor.white, for: .normal)         
    }
}

这对我有用

答案 8 :(得分:1)

跟上下面的解决方案,它可以在iOS 13.0和iOS 12.4上运行,必须在iOS 9.0之前的以前版本上运行。以下解决方案适用于:

  1. 取消按钮颜色(正常状态)。
  2. 取消按钮颜色(禁用状态)。
  3. 搜索栏文本字段背景颜色(正常状态)。

对于目标C:

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:[UIColor whiteColor]]; 

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateDisabled];

[[UIBarButtonItem appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTitleTextAttributes:@{NSBackgroundColorAttributeName: [UIColor whiteColor]} forState:UIControlStateNormal];

上面的代码还解决了iOS 13和iPhone X的UI问题。 我将此代码包含在 didFinishLaunchingWithOptions 函数的 AppDelegate.m 类中,以便可以在整个应用程序中进行更改。

答案 9 :(得分:0)

对于那些希望在Swift中重现相同行为的人:

override func viewWillAppear(animated: Bool) {
    self.searchBar.tintColor = UIColor.whiteColor()

    let view: UIView = self.searchBar.subviews[0] as! UIView
    let subViewsArray = view.subviews

    for (subView: UIView) in subViewsArray as! [UIView] {
        println(subView)
        if subView.isKindOfClass(UITextField){
            subView.tintColor = UIColor.blueColor()
        }
    }

}
相关问题