Search code examples
iosswiftuisearchbarplaceholderuiappearance

UISearchBar.appearance() not working on first launch?


In my AppDelegate, I have customized my UISearchBars in my app (i have a few), so they would match the app colors and such. (The app color isa sort of teal). My code is as follows:

// Set appearance of UISearchBar for the app appearance
        UISearchBar.appearance().tintColor = UIColor.whiteColor()

        UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).tintColor = appColor
        UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).font = UIFont.init(name: "AvenirNext-Medium", size: 17)
        UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.whiteColor()
        UISearchBar.appearance().searchBarStyle = UISearchBarStyle .Minimal
        UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).attributedPlaceholder = NSAttributedString.init(string: "Search...", attributes: [NSForegroundColorAttributeName : UIColor.whiteColor()])
        UISearchBar.appearance().setImage(UIImage(named: "searchIcon"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState .Normal)

After all this, this is what happens on first launch:

first launch

The text is still gray, even though I set it to be white in the AppDelegate.

When I click on the search bar:

clicked

And when I hit cancel:

after cancel hit

This is what it is supposed to look like. I'm not sure why the text is gray before a user interaction. Does anyone have a solution?


Solution

  • You can try

    UILabel.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.whiteColor()
    

    please see if that works