当我在自定义类取消按钮上设置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
}
}
查看是否对您有帮助:
let cancelButtonAttributes: NSDictionary = [NSForegroundColorAttributeName: UIColor .red]
UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as? [String : AnyObject], for: UIControlState.normal)
在您中添加这些行 viewDidload 方法。
尝试以下:
let cancelButtonSearchButton = searchBar.value(forKeyPath: "cancelButton") as? UIButton
cancelButtonSearchButton?.tintColor = UIColor.blue
这进入了您已经设置Uisearchbar的插座的ViewController