如何在Swift中更改UISearchBar上的“取消”按钮的颜色 [英] How to change the colour of the 'Cancel' button on the UISearchBar in Swift

查看:726
本文介绍了如何在Swift中更改UISearchBar上的“取消”按钮的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 UISearchBar 添加到我的 PFQueryTableViewController 的顶部。我已将searchBar的颜色更改为我的应用程序的颜色,但在执行此操作时,它似乎也将其右侧的取消按钮的颜色更改为相同的颜色。理想情况下,我希望颜色为白色。

I have added a UISearchBar to the top of my PFQueryTableViewController. I have changed the colour of the searchBar to be the colour of my app, but in doing this, it seems to have also changed the colour of the 'Cancel' button to the right of it to the same colour. Ideally, I want the colour to be White.

此图显示了目前的状态:

This image shows how it currently looks:

看起来没有取消按钮,但它有与searchBar相同的颜色(你仍然可以按它等)

It looks like there is no 'Cancel' button, but there is, its just the same colour as the searchBar (You can still press it etc)

我有办法将此取消按钮的颜色更改为白色吗?我尝试过的所有东西似乎没什么区别。

Is there a way for me to change the colour of this 'Cancel button to white? Everything i've tried seems to make no difference.

代码我以前用来制作 UISearchBar 这个颜色是:

Code i've used to make the UISearchBar this colour is:

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)

在故事板中我设置了这些:

And in the storyboard i've set these:

最后,为了在SearchBar中制作占位符和文本白色,我使用了:

And finally, to make the placeholder, and text white inside the SearchBar, i've used:

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()

        }

    }

}

感谢您的帮助! :)

推荐答案

环顾四周,这似乎是达到我需要的最佳方式:

Having a look around, this seemed to be the best way to achieve what I needed:

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

这篇关于如何在Swift中更改UISearchBar上的“取消”按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆