当我在我的自定义类上设置barTintColor
属性时,取消按钮变为不可见!它在那里,但它不可见。我的最低兼容性是iOS 8.0,所以我不能使用appearance(whenContainedInInstancesOf: <#T##[UIAppearanceContainer.Type]#>)
,我也尝试循环使用子视图并设置tintColor
,但那也不起作用。我不能我们UIBarButtonItem.appearance().tintColor
,因为我在我的场景中有一个工具栏,这也会改变其他按钮的tintColor。如何更改UISearchBar中取消按钮的颜色
颜色代码是:
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
}
}