点按后搜索栏消失

时间:2015-04-16 18:32:56

标签: ios swift ios8 google-maps-sdk-ios uisearchcontroller

我试图使用谷歌地图实现搜索附近的地方。以下是我到目前为止所做的代码

 override func viewDidLoad() {
    super.viewDidLoad()
//Adding Mapview
    mapView  = GMSMapView(frame: CGRectMake(0, 120, self.view.bounds.width, self.view.bounds.height - 120))

    self.view.addSubview(mapView)
    mapView.mapType = kGMSTypeNormal

    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()

    let resultTableView = UITableView(frame: CGRectMake(10, 100, 300, 60))
    self.searchResultController = UITableViewController()
    self.searchResultController?.tableView = resultTableView
    self.searchResultController?.tableView.dataSource = self
    self.searchResultController?.tableView.delegate = self

      self.searchResultController?.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    self.searchController = UISearchController(searchResultsController: self.searchResultController)
    self.searchController?.searchResultsUpdater = self
    self.searchController?.hidesNavigationBarDuringPresentation = true
    self.searchController?.dimsBackgroundDuringPresentation = false
    self.searchController?.delegate = self
    self.searchController?.searchBar.frame = CGRectMake(0,70,self.view.bounds.width,40)
    self.searchController?.searchBar.placeholder = "Please choose a location"
    self.view.addSubview(self.searchController!.searchBar)
    self.definesPresentationContext = true
}

一切正常,除了searcg bar单击后消失。但它发挥了作用。enter image description here

2 个答案:

答案 0 :(得分:1)

我找到了上述问题的解决方案。

func willPresentSearchController(searchController: UISearchController){

    self.searchResultController?.tableView.addSubview(self.searchController!.searchBar)
}

enter image description here

希望对别人有所帮助!!!

答案 1 :(得分:0)

尝试在[[[self searchController] searchBar] sizeToFit];方法中调用layoutSubviews。它帮助了我。