当我在自定义类上设置barTintColor
属性时取消按钮变得不可见!它在那里,但它是不可见的。我的最低兼容性是iOS 8.0,所以我无法使用appearance(whenContainedInInstancesOf: <#T##[UIAppearanceContainer.Type]#>)
,我也试过循环子视图并设置tintColor
,但这也没有用。我不能UIBarButtonItem.appearance().tintColor
,因为我的场景中有一个工具栏,这也会改变其他按钮的tintColor。
颜色代码是:
static let purpleColor = UIColor(red: CGFloat(70.0/255.0), green: CGFloat(54.0/255.0), blue: CGFloat(224.0/255.0), alpha: CGFloat(1.0))
static let scrollBackgroundColor = UIColor(red: CGFloat(240.0/255.0), green: CGFloat(241.0/255.0), blue: CGFloat(242.0/255.0), alpha: CGFloat(1.0))
我的自定义类就像:
import UIKit
class SPSearchBar: UISearchBar {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
func commonInit() {
tintColor = Config.purpleColor
barTintColor = Config.scrollBackgroundColor
let cancelButton = value(forKey: "cancelButton") as! UIButton
cancelButton.setTitle(NSLocalizedString("Cancel", comment: ""), for: .normal)
cancelButton.setTitleColor(Config.purpleColor, for: .normal)
cancelButton.tintColor = Config.purpleColor
}
}
答案 0 :(得分:1)
看看它是否对您有所帮助:
let cancelButtonAttributes: NSDictionary = [NSForegroundColorAttributeName: UIColor .red]
UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as? [String : AnyObject], for: UIControlState.normal)
在 ViewDidLoad 方法中添加这些行。
答案 1 :(得分:0)
试试这个:
let cancelButtonSearchButton = searchBar.value(forKeyPath: "cancelButton") as? UIButton
cancelButtonSearchButton?.tintColor = UIColor.blue
这将进入ViewController,您可以在其中设置UISearchBar的插座