4

Am trying to implement search nearby places using google maps. The below is the code what I have done so far

 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
}

Everything works fine, except searcg bar disappears after a single tap. But it does its function.enter image description here

Pruthvi Hariharan
  • 551
  • 1
  • 6
  • 23

2 Answers2

1

I found the solution for my above problem.

func willPresentSearchController(searchController: UISearchController){

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

enter image description here

Hope it helps to others!!!

Pruthvi Hariharan
  • 551
  • 1
  • 6
  • 23
0

Try calling [[[self searchController] searchBar] sizeToFit]; inside layoutSubviews method. It helped me.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
igraczech
  • 2,408
  • 3
  • 25
  • 30