Reputation: 49
UISearchbar add navigationController?.navigationBar. How to change "Cancel" color
_searchBar = UISearchBar(frame: CGRect(frame: CGRect(x: 0, y: 0, width: 414, height: 64)))
_searchBar!.delegate = self
_searchBar!.showsCancelButton = true
backgroundColor = .white
barTintColor = .white
tintColor = .red
backgroundImage = UIImage()
_searchBar!.placeholder = "搜索文件"
navigationController?.navigationBar.addSubview(_searchBar!)
This result is "Cancel" color is white. How do I set it to red?
Upvotes: 0
Views: 93
Reputation: 13
let cancelButtonAttributes: NSDictionary = [NSForegroundColorAttributeName: ``UIColor.YOUR COLOR]
UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as?
[String : AnyObject], forState: UIControlState.Normal)
Upvotes: 0
Reputation: 13
Changed the format to readability
let cancelButtonAttributes: NSDictionary = [NSForegroundColorAttributeName: UIColor.YOUR COLOR] UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as? [String : AnyObject], forState: UIControlState.Normal)
Upvotes: 0
Reputation: 14349
Use appearance
function of UIAppearance
module -
Method 1:- Visible cancel button color when searchBar
on load-
let attributes = [NSForegroundColorAttributeName : UIColor.red]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
or -
Method 2:-
Visible cancel button color after searchBar
clicked -
UIBarButtonItem.appearance(whenContainedInInstancesOf:[UISearchBar.self]).tintColor = UIColor.red
Upvotes: 1