如何在Swift中更改UISearchBar上“取消”按钮的颜色?

31

我在我的PFQueryTableViewController顶部添加了一个UISearchBar。我已将搜索栏的颜色更改为与我的应用程序颜色相同,但在这样做时,似乎也将其右侧的“Cancel”按钮的颜色更改为相同的颜色。理想情况下,我希望颜色为白色。

这张图片展示了当前的外观:

看起来好像没有“Cancel”按钮,但实际上是有的,只是与搜索栏的颜色相同(您仍然可以按它等)。

是否有一种方法可以将此“取消”按钮的颜色更改为白色?我尝试过的所有方法似乎都没有任何区别。

我用来使UISearchBar这个颜色的代码是:

UISearchBar.appearance().barTintColor = UIColor(hue: 359/360, saturation: 67/100, brightness: 71/100, alpha: 1)
UISearchBar.appearance().tintColor = UIColor(hue: 359/360, saturation: 67/100, brightness: 71/100, alpha: 1)

在故事板中,我设置了以下内容:

输入图像描述

最后,为了使SearchBar中的占位符和文本变为白色,我使用了:

for subView in self.filmSearchBar.subviews {

    for subsubView in subView.subviews {

        if let searchBarTextField = subsubView as? UITextField {

            searchBarTextField.attributedPlaceholder = NSAttributedString(string: NSLocalizedString("Search Cinevu film reviews", comment: ""), attributes: [NSForegroundColorAttributeName: UIColor.whiteColor()])

            searchBarTextField.textColor = UIColor.whiteColor()

        }

    }

}

感谢任何帮助!:)


之前取消按钮的颜色是什么? - cbay
@cbay 这是默认颜色,类似于浅灰色。 - Nick89
我没有使用搜索栏(我所有的UI都是以编程方式完成的), 但取消按钮的代码在哪里?你只需要像这样使用 cancelButton.backgroundColor = UIColor.greyColor() 来更改颜色。 - cbay
我刚刚在查看文档。showsCancelButton默认为NO。尝试将其设置为YES。如果可能的话,我找不到更改其颜色的位置。 - cbay
http://stackoverflow.com/questions/19651374/how-to-change-cancel-button-tint-color-of-uisearchbar-in-ios7 http://stackoverflow.com/questions/27838084/change-uibarbuttonitem-from-uisearchbar 这些可能会对您有所帮助。 - cbay
我在这个话题中写了一个答案,网址是:https://dev59.com/YGIk5IYBdhLWcg3wgub-。只需使用 SHSearchBar Cocoapod 即可,它不像 UISearchBar 那么让人头疼。 - blackjacx
12个回答

34

环顾四周,这似乎是我达到所需目标的最佳途径:

 let cancelButtonAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
 UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes , for: .normal)

1
如果您正在使用 UISearchController,则必须将此代码插入到 willPresentSearchController:didPresentSearchController: 中。 - LightNight
这会影响应用程序中的其他内容还是只有取消按钮? - Arthur Garza
1
@ArthurGarza 它只影响取消按钮。 - Nick89
这会影响返回按钮标题的颜色。 - Pratyush Pratik

18

使用这行代码来改变取消按钮的颜色:

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.white], for: .normal)

使用 Swift 4.0 在 Xcode 9.2 中检查


1
完美胜利。Xcode 9.4.1,Swift 4.0,iOS 11.4.1。 - eGanges
1
为了简化一下: ... setTitleTextAttributes([.foregroundColor : UIColor.white], for: .normal) - Rufat Mirza

11

基于Nick89的代码,我修改成这样以适应Swift 3.1

let cancelButtonAttributes: [String: AnyObject] = [NSForegroundColorAttributeName: UIColor.white]

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal)

我希望只更改UISearchBar上的按钮而不是所有UIBarButton,因此我使用类似于UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])的方法。


1
同样的答案在 Swift 4 中:let cancelButtonAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key.foregroundColor: UIColor.gray] UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal) - Despotovic

8

你试过使用UISearchBar.appearance().tintColor = UIColor.whiteColor()吗?


您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - Nick89
你有一个可以上传的示例应用程序吗? - David Yang Liu
@DavidYangLiu 它也改变了光标的颜色吗? - iamVishal16

8

Swift 4.2

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)

6

基于其他答案,Swift 4.2版本如下:

let cancelButtonAttributes: [NSAttributedString.Key: Any] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.orange]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal)

5
我会尽力为您翻译。这是需要翻译的内容:

我知道这个问题已经有足够的答案了,但是这里有一个简单的方法可以实现,搜索栏背景颜色和取消按钮背景颜色可以直接在Storyboard属性检查器中更改。

对于搜索栏背景颜色

enter image description here

用于搜索栏取消按钮颜色

enter image description here


3

使用Swift 4.0发布的2017-09-19工具链,这个操作可以运行:

    let cancelButtonAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal)

2
您可以通过设置searchBar的tintcolor来更改取消按钮的颜色。这个功能从Swift 4开始可用,应该可以解决问题。
let searchBar = UISearchBar(frame: )
searchBar.tintColor = .orange

搜索栏取消按钮和光标颜色更改

2

Swift 5

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([.foregroundColor : UIColor.barTint], for: .normal)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接