Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customise GMSAutocompleteViewController for iOS?

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)

enter image description here

like image 963
TechChain Avatar asked Jan 03 '18 07:01

TechChain


4 Answers

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:

enter image description here

like image 111
Sabrina Avatar answered Sep 18 '22 15:09

Sabrina


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.

enter image description here

enter image description here

like image 37
Krunal Avatar answered Sep 21 '22 15:09

Krunal


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]
like image 33
ioopl Avatar answered Sep 20 '22 15:09

ioopl


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
like image 43
Naveen Sharma Avatar answered Sep 18 '22 15:09

Naveen Sharma