I have a
@IBOutlet weak var searchBar: UISearchBar!
and I'm trying to change the font and color of the cancel
button with but it's not working. The code I'm using is the following:
fileprivate static func applyStyle() {
let appearance = UIBarButtonItem.appearance()
let commonAttributes: [NSAttributedString.Key: Any] = [
.font: UIFont.bodySmall,
.foregroundColor: ColorEnum.black,
.kern: NSNumber(value: Float(NLLetterSpacing.bodySmall))
]
appearance.setTitleTextAttributes(commonAttributes, for: .normal)
appearance.setTitleTextAttributes(commonAttributes, for: .highlighted)
appearance.setTitleTextAttributes([
.font: UIFont.bodySmall,
.foregroundColor: ColorEnum.pinkishGrey,
.kern: NSNumber(value: Float(NLLetterSpacing.bodySmall))
], for: .disabled)
}
However, I also have a weak var searchController: UISearchController?
and the code it's working for it's searchController.searchBar
.
Adding
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(commonAttributes, for: .normal)
should do the trick, then you could easily get rid of the appearance