【发布时间】:2014-09-20 09:51:42
【问题描述】:
我希望我的 searchBar 的色调为白色(意味着取消按钮为白色)。当色调为白色时,光标不可见。有没有办法单独设置光标颜色?
【问题讨论】:
-
我在这里发布了一个带有详细截图的解决方案:stackoverflow.com/a/42444940/4754881
标签: ios7 uisearchbar tintcolor
我希望我的 searchBar 的色调为白色(意味着取消按钮为白色)。当色调为白色时,光标不可见。有没有办法单独设置光标颜色?
【问题讨论】:
标签: ios7 uisearchbar tintcolor
将您的色调颜色设置为您希望取消按钮的颜色,然后使用UIAppearance Protocol 将文本字段上的色调颜色更改为您希望光标的颜色。例如:
[self.searchBar setTintColor:[UIColor whiteColor]];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTintColor:[UIColor darkGrayColor]];
【讨论】:
searchController.searchBar.tintColor = .white
UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).tintColor = .black
请注意,searchBar 不能是可选的。
【讨论】:
如果你喜欢 Swift 中功能性但令人讨厌的单行代码,我得到了 Benjamin 的 for 循环:
searchController.searchBar.tintColor = UIColor.whiteColor()
searchController.searchBar.subviews[0].subviews.flatMap(){ $0 as? UITextField }.first?.tintColor = UIColor.blueColor()
【讨论】:
在 Swift 5 中最简单:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .black
【讨论】:
使用 for-where 语法的紧凑型 Swift 2.0 解决方案(无需中断循环):
// Make SearchBar's tint color white to get white cancel button.
searchBar.tintColor = UIColor.white()
// Loop into it's subviews and find TextField, change tint color to something else.
for subView in searchBar.subviews[0].subviews where subView.isKindOfClass(UITextField) {
subView.tintColor = UIColor.darkTextColor()
}
【讨论】:
这似乎对我也很有效。
searchController.searchBar.tintColor = UIColor.whiteColor()
UITextField.appearanceWhenContainedInInstancesOfClasses([searchController.searchBar.dynamicType]).tintColor = UIColor.blackColor()
【讨论】:
对于那些希望在 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()
}
}
}
【讨论】:
我将使用以下代码向 UISearchBar 添加扩展。
extension UISearchBar {
var cursorColor: UIColor! {
set {
for subView in self.subviews[0].subviews where ((subView as? UITextField) != nil) {
subView.tintColor = newValue
}
}
get {
for subView in self.subviews[0].subviews where ((subView as? UITextField) != nil) {
return subView.tintColor
}
// Return default tintColor
return UIColor.eightBit(red: 1, green: 122, blue: 255, alpha: 100)
}
}
}
【讨论】:
为取消按钮和 textField 设置不同的 tintColor 最简单的方法,使用这个:
[self.searchBar setTintColor:[UIColor whiteColor]];
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:UIColor.blueColor];
【讨论】:
这是最简单的解决方案。
let textField = self.searchBar.value(forKey: "searchField") as! UITextField
textField.tintColor = UIColor.white
【讨论】:
let textField = self.searchBar.value(forKey: "searchField") as? UITextField; textField?.tintColor = UIColor.white 避免强制展开,从而避免任何不必要的崩溃。
这是 Felipe 答案的“功能”版本 (Swift 4.2)
// Make SearchBar's tint color white to get white cancel button.
searchBar.tintColor = .white
// Get the TextField subviews, change tint color to something else.
if let textFields = searchBar.subviews.first?.subviews.compactMap({ $0 as? UITextField }) {
textFields.forEach { $0.tintColor = UIColor.darkGray }
}
【讨论】: