I am using Google autocomplete placekicker in ios. It shows me controller with native design. I want to customise it's navigation bar colour.But I am not able to do it. Below is the code
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.tintColor = UIColor.red
autocompleteController.navigationController?.navigationBar.barTintColor = Constant.AppColor.navigationColor
autocompleteController.delegate = self
self.present(autocompleteController, animated: true, completion: nil)
I have written this code for adapting light/dark mode:
Swift
let controller:GMSAutocompleteViewController! = GMSAutocompleteViewController()
if #available(iOS 13.0, *) {
if UIScreen.mainScreen.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark {
controller.primaryTextColor = UIColor.whiteColor
controller.secondaryTextColor = UIColor.lightGrayColor
controller.tableCellSeparatorColor = UIColor.lightGrayColor
controller.tableCellBackgroundColor = UIColor.darkGrayColor
} else {
controller.primaryTextColor = UIColor.blackColor
controller.secondaryTextColor = UIColor.lightGrayColor
controller.tableCellSeparatorColor = UIColor.lightGrayColor
controller.tableCellBackgroundColor = UIColor.whiteColor
}
}
Objective-C
GMSAutocompleteViewController *controller = [[GMSAutocompleteViewController alloc] init];
if (@available(iOS 13.0, *)) {
if(UIScreen.mainScreen.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark ){
controller.primaryTextColor = UIColor.whiteColor;
controller.secondaryTextColor = UIColor.lightGrayColor;
controller.tableCellSeparatorColor = UIColor.lightGrayColor;
controller.tableCellBackgroundColor = UIColor.darkGrayColor;
} else {
controller.primaryTextColor = UIColor.blackColor;
controller.secondaryTextColor = UIColor.lightGrayColor;
controller.tableCellSeparatorColor = UIColor.lightGrayColor;
controller.tableCellBackgroundColor = UIColor.whiteColor;
}
}
Result:
Google Place Autocomplete document can help you.
According to document, use UIAppearanceProtocol to customise visual theme.
Look at section "Customize text and background colors" in this document.
I was able to customise some elements in Swift like so :
// Sets the background of results - top line
autocompleteController.primaryTextColor = UIColor.black
// Sets the background of results - second line
autocompleteController.secondaryTextColor = Color.black
// Sets the text color of the text in search field
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white]
Please use the below code to support light and dark mode in iOS.
let autocompleteController = GMSAutocompleteViewController()
autocompleteController.delegate = self
if #available(iOS 13.0, *) {
if UIScreen.main.traitCollection.userInterfaceStyle == .dark {
autocompleteController.primaryTextColor = UIColor.white
autocompleteController.secondaryTextColor = UIColor.lightGray
autocompleteController.tableCellSeparatorColor = UIColor.lightGray
autocompleteController.tableCellBackgroundColor = UIColor.darkGray
} else {
autocompleteController.primaryTextColor = UIColor.black
autocompleteController.secondaryTextColor = UIColor.lightGray
autocompleteController.tableCellSeparatorColor = UIColor.lightGray
autocompleteController.tableCellBackgroundColor = UIColor.white
}
}
To access the full code, please check out the below link.
https://gist.github.com/imnaveensharma/fb41063c1f858da15199ee7545f51422
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With