8

我在 ios 中使用 Google 自动完成占位符。它向我展示了具有原生设计的控制器。我想自定义它的导航栏颜色。但我做不到。下面是代码

        let autocompleteController = GMSAutocompleteViewController()
        autocompleteController.tintColor = UIColor.red
        autocompleteController.navigationController?.navigationBar.barTintColor = Constant.AppColor.navigationColor
        autocompleteController.delegate = self
        self.present(autocompleteController, animated: true, completion: nil)

在此处输入图像描述

4

6 回答 6

8

我编写了这段代码来适应明暗模式:

迅速

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;
   }
}

结果:

在此处输入图像描述

于 2019-11-27T14:02:11.767 回答
3

我能够像这样在 Swift 中自定义一些元素:

     // 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]
于 2018-03-01T11:38:03.933 回答
3

Google Place Autocomplete文档可以为您提供帮助。

根据文档,使用UIAppearanceProtocol自定义视觉主题。

查看本文档中的“自定义文本和背景颜色”部分。

在此处输入图像描述

在此处输入图像描述

于 2018-01-03T07:47:05.793 回答
1
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
于 2020-10-28T08:11:39.417 回答
0

更新 Swift 5+ 的代码

完整的代码将如下所示。

// Present the Autocomplete view controller 
    @objc func autoCompleteClicked() {
        
        let autocompleteController:GMSAutocompleteViewController! = GMSAutocompleteViewController()
        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
           }
        }
        
        
        
     // let autocompleteController = GMSAutocompleteViewController()
      autocompleteController.delegate = self

      // Specify the place data types to return.
      let fields: GMSPlaceField = GMSPlaceField(rawValue:UInt(GMSPlaceField.all.rawValue))
  
        
        
      autocompleteController.placeFields = fields

      // Specify a filter.
      let filter = GMSAutocompleteFilter()
      filter.type = .address
      autocompleteController.autocompleteFilter = filter

      // Display the autocomplete view controller.
      present(autocompleteController, animated: true, completion: nil)
    }

我更新了@Sabrina 的代码

于 2021-02-26T10:58:32.247 回答
0

iOS > 13 for Light and Dark Mode 的更好解决方案:

autocompleteController.primaryTextColor = UIColor.label
autocompleteController.secondaryTextColor = UIColor.secondaryLabel
autocompleteController.tableCellSeparatorColor = UIColor.separator
autocompleteController.tableCellBackgroundColor = UIColor.systemBackground
于 2020-07-15T00:12:06.540 回答