简体   繁体   中英

Changing the tint color is not changing the font of the "cancel" button inside the search bar

I have a UIView called SearchView, and I am adding the searchController.searchBar as a subview of the UIView.

Inside viewDidLoad(), I am changing the search bar in my UISearchController like so:

    //makes background transparent
    searchController.searchBar.backgroundImage = UIImage()

    //makes magnifying glass white
    let iconView:UIImageView = tf!.leftView as! UIImageView
    iconView.image = iconView.image?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
    iconView.tintColor = UIColor.whiteColor()

    //changes text color to white
    let tf = searchController.searchBar.valueForKey("searchField") as? UITextField
    let attributedString = NSAttributedString(string: "", attributes:[NSForegroundColorAttributeName : UIColor.whiteColor()]) 
    tf!.attributedPlaceholder = attributedString
    tf!.textColor = UIColor.whiteColor()
    //changes search field background color
    tf!.backgroundColor = UIColor(red: 82/255, green: 91/255, blue: 93/255, alpha: 1) 

    //HERE: should make the cancel button font white...
    searchController.searchBar.tintColor = UIColor.whiteColor()

    searchView.addSubview(searchController.searchBar)

And in the AppDelegate, I have

UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).tintColor = UIColor.whiteColor()

In your app delegate at launch time, say this:

UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).tintColor = UIColor.whiteColor()

Note that this will have an effect only when there is editing going on in the search bar. If there is no editing in the search bar, there is nothing to cancel, so the Cancel button is dimmed and has no color (it's a kind of grey based on the tint color).

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM