[英]UISearchBar cancel button color?
当我将 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 提交应用程序时这可能是一个问题。
我使用了这样的东西并与我一起工作:
[[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
您的解决方案的问题在于代码假设 objectAtIndex:3 是取消按钮。 这不仅会生成编译器警告,而且如果您以编程方式显示“取消”按钮(例如使用[searchBar setShowsCancelButton:YES]
,则可能会导致应用程序崩溃。
一个更简单的解决方案是在 ViewDidLoad() 中设置整个搜索栏的样式,使用:
searchBar.tintColor = [UIColor colorWithWhite:0.3 alpha:1.0];
这会覆盖在界面生成器中设置的样式,但也会将取消按钮的颜色更改为与整个栏的颜色相同(尽管不幸的是,它不允许您独立设置取消按钮的样式。
试试这个看看:(我用 Swift 4.1 - Xcode 9.3-beta4测试了下面的代码)
@IBOutlet weak var sbSearchBar: UISearchBar!
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)
}
在 Swift 4.2 中
let appearance = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])
appearance.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor(named: "goldColor")!], for: .normal)
这对我有用。 谢谢@Tim Semple
对于 iOS 10:
UISearchBar.appearance().tintColor = UIColor.red //cancel button color
UISearchBar.appearance().barTintColor = UIColor.blue //background button color
这是上面针对 Swift 3 的Hossam Ghareeb 回答的更新版本:
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)
我已经采用了Benjamin 的答案,并将其与安全Array
查找相结合,以生成一个简短但安全的功能版本:
searchController.searchBar.tintColor = UIColor.whiteColor()
(searchController.searchBar.subviews[safe: 0]?.subviews as? [UIView])?
.filter({$0.isKindOfClass(UITextField)})
.map({$0.tintColor = .lightGrayColor()})
这会导致在键入灰色时将取消按钮和光标着色为白色。 否则它会是白色的,因此看不到。 searchController
是UISearchController
类型的对象。 如果有人想在结果控制器中使用它,请将其替换为self
。
safe:
下标的实现是nkukushkin 的回答:
extension Array {
subscript(safe index: Int) -> T? {
return indices(self) ~= index ? self[index] : nil
}
}
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)
}
}
这对我有用
提出了以下解决方案,它也适用于 iOS 13.0 和 iOS 12.4,必须适用于之前的版本,直到 iOS 9.0。 以下解决方案适用于:
对于目标 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 问题。我将此代码包含在我的AppDelegate.m类中的didFinishLaunchingWithOptions函数中,以便可以在整个应用程序中完成更改。
对于那些希望在 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()
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.