问题
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.
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()
回答1:
For those who are looking for Swift 4 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]
来源:https://stackoverflow.com/questions/39912800/gmsautocompleteviewcontroller-ios-how-to-change-the-text-color-in-the-searchbar