ben fo ben fo - 11 months ago 81
iOS Question

Add full searchBar to navigation

I am adding a search bar to my navigation bar like this

searchBar.showsCancelButton = false
searchBar.placeholder = "Search"
searchBar.delegate = self
searchBar.enablesReturnKeyAutomatically = true
self.navigationItem.titleView = searchBar

but i have bar button items is there a way to hide this and make the searchBar full length

Joe Joe
Answer Source

Try this Code:

  class ViewController: UIViewController,UISearchControllerDelegate, UISearchResultsUpdating, UISearchBarDelegate {

  var resultSearchController : UISearchController!

  override func viewDidLoad() {
    // Do any additional setup after loading the view, typically from a nib.

    self.resultSearchController = UISearchController(searchResultsController:  nil)
    self.resultSearchController.searchResultsUpdater = self
    self.resultSearchController.delegate = self
    self.resultSearchController.searchBar.delegate = self
    self.resultSearchController.hidesNavigationBarDuringPresentation = false
    self.resultSearchController.dimsBackgroundDuringPresentation = true
    self.definesPresentationContext = true 

    self.navigationItem.titleView = resultSearchController.searchBar

func updateSearchResults(for searchController: UISearchController) {

    // You have to implement search delegate method here to make it work.