Reputation: 1261
I am searching for change color of GMSAutocompleteViewController
Here is what I am getting:
I want the textfield to be white with white cancel button.
Upvotes: 4
Views: 5915
Reputation: 1555
I face the same issue while working on Google Maps API and found my solution by the following line of code:
[[UITextField appearanceWhenContainedIn:[<Object of your Google Maps API class(like GMSAutocompleteViewController)>.searchDisplayController.searchBar class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
Add the line above after you make an object of your Google Maps API class and before you present the class.
The accepted answer above will also work for this scenario and is appreciable, but that line of code will make default black color for the text in the search bar.
By using my answer, one can change the text color to any custom color available in iOS.
Upvotes: 0
Reputation: 1261
Swift 3
@IBAction func searchCityBtn(_ sender: Any) {
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self
UINavigationBar.appearance().barTintColor = UIColor(red: 44.0/255, green: 44.0/255, blue: 49.0/255, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.white
UISearchBar.appearance().setTextColor(UIColor.white)
UISearchBar.appearance().barStyle = UIBarStyle.default
UISearchBar.appearance().setPlaceholderTextColor(.white)
UISearchBar.appearance().setSearchImageColor(.white)
self.present(autocompleteController, animated: true, completion: nil)
}
more info about what you can edit here.... Thank god they updated their framework and doc! Cheers!
Upvotes: 6
Reputation: 23
Change color of GMSAutocompleteViewController
Do following things:
-Create a new NSObject class to set the attributes of UITextField
.h file
@interface HelperClass : NSObject
+(void)setTextFieldAttributes;
@end
.m file
@implementation HelperClass
+(void)setTextFieldAttributes{
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil]
setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
[[UITextField appearance] setTintColor:[UIColor whiteColor]];
}
@end
and then in your ControllerClass: GMSAutocompleteViewController
UItextFieldGoogleHelperClass.setTextFieldAttributes()
will do the trick, worked for me.
Upvotes: 0
Reputation: 1
Add following code inside didFinishLaunchingWithOptions
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
if #available(iOS 9.0, *) {
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
} else {
// Fallback on earlier versions
}
Upvotes: 0
Reputation: 10889
The latest release of the Google Places SDK for iOS (1.13) has added extra support for customising colors in GMSAutocompleteViewController
. There's a good overview in the online guide.
Upvotes: 0
Reputation: 4950
A view controller's view can be accessed through it's view property which is just a regular UIView. UIView have a backgroundColor property which is a UIColor and controls the color of the view.
Here is a sample code for changing background color:
self.view.backgroundColor = UIColor.yellowColor()
Upvotes: -1