目前,我按照这些步骤实施了Google地方信息自动填充搜索栏。
https://developers.google.com/places/ios-api/autocomplete#add_an_autocomplete_ui_control
到目前为止一切顺利,但是当我点击搜索栏时它会向下移动,这就是我不想要的。
To(点击时):
我当前的代码与教程中的代码完全相同(我之后也尝试过修复bug,不起作用)
var resultsViewController: GMSAutocompleteResultsViewController?
var searchController: UISearchController?
var resultView: UITextView?
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Event-Filter";
resultsViewController = GMSAutocompleteResultsViewController()
resultsViewController?.delegate = self
searchController = UISearchController(searchResultsController: resultsViewController)
searchController?.searchResultsUpdater = resultsViewController
let subView = UIView(frame: CGRect(x: 0, y: 0.0, width: self.view.frame.width, height: 45.0))
subView.addSubview((searchController?.searchBar)!)
view.addSubview(subView)
searchController?.searchBar.sizeToFit()
searchController?.searchBar.placeholder = "Enter a specific place!";
navigationController?.navigationBar.isTranslucent = false
searchController?.hidesNavigationBarDuringPresentation = false
// When UISearchController presents the results view, present it in
// this view controller, not one further up the chain.
definesPresentationContext = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//Handle User Selection
func resultsController(_ resultsController: GMSAutocompleteResultsViewController, didAutocompleteWith place: GMSPlace) {
searchController?.isActive = false
searchController?.searchBar.text = place.formattedAddress;
print("Place name: \(place.name)")
print("Place address: \(place.formattedAddress ?? "not found")")
print("Place lat:" + String(place.coordinate.latitude))
}
func resultsController(_ resultsController: GMSAutocompleteResultsViewController,
didFailAutocompleteWithError error: Error){
let backend = BackendService()
backend.alert(view: self, message: error.localizedDescription, title: "Something went wrong!")
}
// Turn the network activity indicator on and off again.
func didRequestAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
func didUpdateAutocompletePredictions(forResultsController resultsController: GMSAutocompleteResultsViewController) {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
答案 0 :(得分:-1)
好吧,我遇到了同样的问题,并且通过使用以下代码行使其正常工作:
definesPresentationContext = false
我测试了很多,它对我来说很完美。