4

I'm using a GMSAutocompleteViewController and want to change the textColor in the searchBar but can't find a way to do it, I managed to change the some colors but not the searchBar text.

enter image description here

I've tried following code but the color won't change:

        acController.searchBarController?.searchBar.tintColor = UIColor.whiteColor()
        acController.searchBarController?.searchBar.textColor = UIColor.whiteColor()

        acController.searchBarController?.searchBar.textField.textColor = UIColor.whiteColor()
        acController.searchBarController?.searchBar.textField.tintColor = UIColor.whiteColor()
        acController.searchBarController?.searchBar.textField.backgroundColor = UIColor.whiteColor()

        acController.searchDisplayController?.searchBar.setTextColor(UIColor.whiteColor())
        acController.searchDisplayController?.searchBar.tintColor = UIColor.whiteColor()

        // changes the color of the sugested places
        acController.primaryTextColor = UIColor.whiteColor()
        acController.secondaryTextColor = UIColor.whiteColor()
Steven B.
  • 1,429
  • 2
  • 19
  • 38

1 Answers1

5

For those who are looking for Swift4 version, following code worked for me in Swift 4 (Xcode 9.4 & iOS 11)

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]

For Swift 5

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
Anand
  • 5,323
  • 5
  • 44
  • 58
  • 1
    This worked for me. Worth noting it will change it across the entire app. For Swift 5 it has changed slightly. ```UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]``` – Jeremiah Jan 28 '20 at 16:05
  • 1
    @Jeremiah: Thanks for pointing it. I have updated answer with your suggestion. – Anand Jan 30 '20 at 07:42