-2

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()
Michael McKenna
  • 811
  • 1
  • 11
  • 24
  • see this http://stackoverflow.com/questions/2787481/uisearchbar-cancel-button-color – NSLog Mar 15 '16 at 19:26
  • check apple's example https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html and just add searchController.searchBar.tintColor = UIColor.whiteColor() in viewDidLoad method – Muhammad Adnan Mar 15 '16 at 19:35
  • @Pran I used (UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self])).tintColor = UIColor.whiteColor() and that didn't work. Muhammad, I tried doing that on its own, and then I tried doing it in combination with Pran's answer and the cancel button font still won't change. This sucks. – Michael McKenna Mar 15 '16 at 19:35
  • @MuhammadAdnan As you can see, I have already done searchController.searchBar.tintColor = UIColor.whiteColor() which isn't working. – Michael McKenna Mar 15 '16 at 19:36
  • @MuhammadAdnan That ends up making the background white when I want it to be transparent. Also, the cancel button text is still a light grey color. – Michael McKenna Mar 15 '16 at 19:40

1 Answers1

1

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

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Thank you for your answer, and I'm sure this will still help somebody...but that's still not changing the cancel button font on mine. I'm not sure why I am getting downvoted. I'll update my question with what I have tried since people started answering. – Michael McKenna Mar 15 '16 at 19:48
  • Well, obviously I don't know what else you're doing. My answer includes a screen shot that shows that it _does_ work. As I explained in my answer, the "light grey" is only when you are not _editing_ in the search bar, and there is nothing you can do about that. – matt Mar 15 '16 at 19:52
  • While editing, the font is still not white for me. I posted all of the relevant code, so I'm not hiding anything. Not sure what's going on. – Michael McKenna Mar 15 '16 at 19:53
  • UIBarButtonItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName:UIColor(red: 72/255, green: 79/255, blue: 79/255, alpha: 1), NSFontAttributeName: UIFont(name: "Open Sans", size: 14)!], forState: UIControlState.Normal) I thought your code would override this code which was placed before. I commented this code out and yours worked like a charm. Sorry everybody...didn't think to check the AppDelegate. – Michael McKenna Mar 15 '16 at 20:02