如何在Google Place AutocompleteViewController中的GMSAutocompleteViewController中设置文本

Nal*_*uza 8 google-maps autocomplete google-places-api google-picker swift

我需要帮助GMSAutocompleteViewController在我的应用程序中打开时在searchBar中设置文本.我GMSAutocompleteViewControllerGoogle Place AutocompleteViewController中使用.

在此输入图像描述

Daf*_*man 7

我通过结合@Youngjin和@ Exception的Swift 4和Google Places 2.6.0的答案得到了一个有效的解决方案

要访问GMSAutocompleteViewController搜索栏:

let views = gmsAutoCompleteViewController.view.subviews
let subviewsOfSubview = views.first!.subviews
let subOfNavTransitionView = subviewsOfSubview[1].subviews
let subOfContentView = subOfNavTransitionView[2].subviews     
let searchBar = subOfContentView[0] as! UISearchBar
Run Code Online (Sandbox Code Playgroud)

然后设置文本并自动搜索:

searchBar.text = "Your address"
searchBar.delegate?.searchBar?(searchBar, textDidChange: "Your address") // This performs the automatic searching.
Run Code Online (Sandbox Code Playgroud)

我发现在尝试从didRequestAutocompletePredictions(_ viewController:GMSAutocompleteViewController)中设置文本时收到了EXC_BAD_ACCESS错误.

所以我把这段代码放在我提供autoCompleteController的完成块中,它可以正常工作.

将它们组合在一起:

let gmsAutoCompleteViewController = GMSAutocompleteViewController()
gmsAutoCompleteViewController.delegate = self

present(gmsAutoCompleteViewController, animated: true) {
    let views = gmsAutoCompleteViewController.view.subviews
    let subviewsOfSubview = views.first!.subviews
    let subOfNavTransitionView = subviewsOfSubview[1].subviews
    let subOfContentView = subOfNavTransitionView[2].subviews
    let searchBar = subOfContentView[0] as! UISearchBar
    searchBar.text = "Your address"
    searchBar.delegate?.searchBar?(searchBar, textDidChange: "Your address")
}
Run Code Online (Sandbox Code Playgroud)

编辑:我发现这个解决方案似乎只适用于iOS 11. let searchBar = subOfContentView[0] as! UISearchBar 在iOS 10上会失败,可能还会降低版本.


You*_*Jin 6

@Cools

在Swift 3中,它似乎有不同的层次结构.我已经深入VC并获得了这个位置.

func didRequestAutocompletePredictions(_ viewController: GMSAutocompleteViewController) {
    let views = viewController.view.subviews
    let subviewsOfSubview = views.first!.subviews
    let subOfNavTransitionView = subviewsOfSubview[1].subviews
    let subOfContentView = subOfNavTransitionView[2].subviews
    let searchBar = subOfContentView[0] as! UISearchBar
    searchBar.text = "YOUR_TEXT"
}
Run Code Online (Sandbox Code Playgroud)

但是,它有一个故障,它不会自动搜索.在我得到解决方案之后,我将再次编辑.


dav*_*ynn 6

好吧,我不一定推荐这种做法,因为如果你想要更多的控制,你应该使用 UISearchResults 控制器。但是,这是一个更快速的版本:

//Where viewController is the GMSAutocompleteViewController
if let searchBar = (viewController.view.subviews
   .flatMap { $0.subviews }
   .flatMap { $0.subviews }
   .flatMap { $0.subviews }
   .filter { $0 == $0 as? UISearchBar}).first as? UISearchBar {
            searchBar.text = "YOUR_TEXT_HERE"
            searchBar.delegate?.searchBar?(searchBar, textDidChange: "YOUR_TEXT_HERE") // to get the autoComplete Response

    }
Run Code Online (Sandbox Code Playgroud)

这种方法的好处是您不必确切知道 SearchBar 的位置。它基本上是试图将所有子视图展平为一个数组,并从数组中过滤掉 SearchBar。如果 Google 决定更改 SearchBar 的隐藏位置,这种方法仍然可以找到它。问题是你仍然需要知道子视图有多少层。您可以设置一个递归函数来处理这个问题。


Mr.*_*bot 1

我不太熟悉快速编码,但基于文档 -自定义文本和背景颜色

您可以在自动完成 UI 控件中设置所有文本和背景的颜色,以使小部件更紧密地匹配应用程序的视觉外观。设置 UI 控件颜色有两种方法:

文档指出可以使用(UIAppearance 协议或 SDK 方法)设置搜索栏文本颜色、搜索栏色调颜色和搜索栏占位符文本颜色(默认搜索文本)的样式。

希望这些信息有帮助。