ios 如何在 Swift 中更改 UISearchBar 上“取消”按钮的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35302760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the colour of the 'Cancel' button on the UISearchBar in Swift
提问by Nick89
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.
我UISearchBar
在我的PFQueryTableViewController
. 我已将 searchBar 的颜色更改为我的应用程序的颜色,但在这样做时,它似乎也将其右侧的“取消”按钮的颜色更改为相同的颜色。理想情况下,我希望颜色为白色。
This image shows how it currently looks:
这张图片显示了它目前的样子:
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.
有没有办法将这个“取消”按钮的颜色更改为白色?我尝试过的一切似乎都没有区别。
Code i've used to make the UISearchBar
this colour is:
我用来制作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)
And in the storyboard i've set these:
在故事板中,我设置了这些:
And finally, to make the placeholder, and text white inside the SearchBar, i've used:
最后,为了使 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()
}
}
}
Thanks for any help! :)
谢谢你的帮助!:)
回答by Nick89
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)
回答by Incredible_Dev
Use this single line in code to change color of cancel button:
在代码中使用这一行来更改取消按钮的颜色:
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.white], for: .normal)
Checked in Xcode 9.2 with Swift 4.0
使用 Swift 4.0 在 Xcode 9.2 中检查
回答by saturngod
Base on Nick89's code , I changed like that for Swift 3.1
基于 Nick89 的代码,我对 Swift 3.1 进行了类似的更改
let cancelButtonAttributes: [String: AnyObject] = [NSForegroundColorAttributeName: UIColor.white]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal)
I want to change on UISearchBar
only instead of all UIBarButton, so I'm using like UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])
我只想更改UISearchBar
而不是所有 UIBarButton,所以我正在使用UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])
回答by David Yang Liu
did you try UISearchBar.appearance().tintColor = UIColor.whiteColor()
你试过 UISearchBar.appearance().tintColor = UIColor.whiteColor()
回答by Abhishek Jain
Swift 4.2
斯威夫特 4.2
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)
回答by Ram
I know this question does have enough answers but here is simple way to achieve, SearchBar Background colour and Cancel button background colour we can change directly it in storyboard Attributes inspector.
我知道这个问题确实有足够的答案,但这里有一个简单的方法来实现,搜索栏背景颜色和取消按钮背景颜色我们可以直接在情节提要属性检查器中更改它。
for SearchBar background colour
搜索栏背景颜色
for SearchBar cancel button colour
SearchBar 取消按钮颜色
回答by Matt
The Swift 4.2 version, based on other answers:
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)
回答by Tino
With the Swift 4.0 RELEASE 2017-09-19 toolchain, this worked:
使用 Swift 4.0 RELEASE 2017-09-19 工具链,这有效:
let cancelButtonAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(cancelButtonAttributes, for: .normal)
回答by lexxnew
Swift 5
斯威夫特 5
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([.foregroundColor : UIColor.barTint], for: .normal)
回答by Lok SN
All the above answers didn't work for me. (Got 'Type 'NSAttributedString.Key' (aka 'NSString') has no member 'foregroundColor'' error)
以上所有答案对我都不起作用。(得到'类型'NSAttributedString.Key'(又名'NSString')没有成员'foregroundColor''错误)
Maybe because I'm at Swift 3...
也许是因为我在 Swift 3 ......
Here's the slightly-modified code that worked for me:-
这是对我有用的稍微修改的代码:-
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSForegroundColorAttributeName : .black], for: .normal)
Note:-
笔记:-
If you are using UISearchController, insert this code to 'willPresentSearchController:' or 'didPresentSearchController:'
如果您使用 UISearchController,请将此代码插入到 'willPresentSearchController:' 或 'didPresentSearchController:'