如何为iOS自定义GMSAutocompleteViewController?

时间:2018-01-03 07:11:27

标签: ios swift xcode uicolor gmsautocomplete

我在ios中使用Google自动填充placekicker。它向我展示了原生设计的控制器。我想自定义它的导航栏colour.But我无法做到。 以下是代码

        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

6 个答案:

答案 0 :(得分:2)

我已经为适应明暗模式编写了以下代码:

快速

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

结果:

enter image description here

答案 1 :(得分:1)

我能够在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]

答案 2 :(得分: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

答案 3 :(得分:0)

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

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

查看" 自定义文字和背景颜色"在这份文件中。

enter image description here

enter image description here

答案 4 :(得分:0)

适用于iOS的更好解决方案> 13不支持明暗模式:

TEXT

答案 5 :(得分: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 的代码