Minebomber
Minebomber

Reputation: 1201

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?

Upvotes: 1

Views: 703

Answers (1)

Nat
Nat

Reputation: 36

You can try

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

please see if that works

Upvotes: 2

Related Questions