Ale*_*lex 5 google-maps uisearchbar ios swift uisearchcontroller
我正在尝试创建一个屏幕,其中将有一个覆盖整个屏幕的Google地图和顶部显示所选地址(标记指向的位置)的按钮.我希望当用户点击该按钮以显示UISearchBar时,他可以使用Google Places API搜索位置.
我一直在关注以下教程:https: //www.youtube.com/watch?v = gRQUoHleCGM
不幸的是,在我的情况下,搜索栏没有出现(虽然当我按下按钮时背景变暗).
以下是我按下按钮之前的一些截图.
这是我的视图控制器的代码.
class ChooseLocationViewController: UIViewController, GMSMapViewDelegate, UISearchBarDelegate, UISearchControllerDelegate {
let geocoder = GMSGeocoder()
var mapView: GMSMapView = GMSMapView.mapWithFrame(CGRectZero, camera:GMSCameraPosition.cameraWithLatitude(51.515339, longitude: -0.141838, zoom: 16))
var addressButton: UIButton = UIButton()
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
let marker = UIImageView(frame: CGRect(x: 20, y: 20, width: 50, height: 50))
marker.image = UIImage(named: "default-marker")
marker.center.x = self.view.center.x
marker.center.y = self.view.center.y - (self.navigationController?.navigationBar.frame.size.height)!
addressButton = UIButton(frame: CGRect(x: 20, y: 20, width: self.view.frame.size.width - 25, height: 47))
addressButton.center.x = self.view.center.x
addressButton.backgroundColor = UIColor(red: 248/255, green: 248/255, blue: 248/255, alpha: 1.0)
addressButton.layer.borderColor = UIColor(red: 178/255, green: 178/255, blue: 178/255, alpha: 1.0).CGColor
addressButton.layer.borderWidth = 1.0
addressButton.layer.cornerRadius = 5
addressButton.setTitleColor(UIColor(red: 68/255, green: 68/255, blue: 68/255, alpha: 1.0), forState: .Normal)
addressButton.titleLabel?.font = UIFont.systemFontOfSize(13)
addressButton.addTarget(self, action: "showLocationSearch", forControlEvents: .TouchDown)
self.view = mapView
self.view.addSubview(marker)
self.view.addSubview(addressButton)
}
func showLocationSearch() {
let searchController = UISearchController(searchResultsController: nil)
searchController.delegate = self
searchController.searchBar.delegate = self
self.presentViewController(searchController, animated: true, completion: nil)
}
func willPresentSearchController(searchController: UISearchController) {
self.navigationController?.extendedLayoutIncludesOpaqueBars = true
}
func willDismissSearchController(searchController: UISearchController) {
self.navigationController?.extendedLayoutIncludesOpaqueBars = false
}
func mapView(mapView: GMSMapView, idleAtCameraPosition position: GMSCameraPosition) {
let coordinate = position.target
geocoder.reverseGeocodeCoordinate(coordinate) { response, error in
if let address = response?.firstResult() {
if let lines = address.lines {
if (lines.count > 0) {
self.addressButton.setTitle(lines[0], forState: .Normal)
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
另外,我正在更改extendedLayoutIncludesOpaqueBars的原因是因为我注意到在我有搜索栏的其他视图中,这是正确显示的必要条件.在这种情况下,我得到完全相同的行为(搜索栏没有显示)我是否这样做.
我究竟做错了什么?有没有人有类似的问题?
| 归档时间: |
|
| 查看次数: |
2040 次 |
| 最近记录: |