Search code examples
iosswiftuisearchcontroller

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()

Solution

  • 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).

    enter image description here