Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GMSAutocompleteViewController iOS, how to change the text color in the searchBar [duplicate]

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()
like image 355
Steven B. Avatar asked Oct 29 '25 15:10

Steven B.


1 Answers

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]
like image 168
Anand Avatar answered Oct 31 '25 05:10

Anand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!