Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search bar not showing up on the navigation bar iOS 11

I am pushing a viewController where I want a searchBar, but search bar is not showing at all. Below is the code. Am I missing something?

var searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.placeholder = "Search here..."
definesPresentationContext = true
searchController.searchBar.delegate = self
searchController.searchBar.sizeToFit()
if #available(iOS 11.0, *) {

    self.navigationItem.searchController = searchController
} else {
    // Fallback on earlier versions
    navigationItem.titleView = searchController.searchBar
    navigationItem.titleView?.layoutSubviews()
}
like image 574
Ajay Kumar Avatar asked Jul 04 '18 15:07

Ajay Kumar


1 Answers

You need to add this line to your code:

navigationItem.hidesSearchBarWhenScrolling = false

That removes hiding searchBar while scrolling and shows it on pushing your view controller.

like image 117
Oleh Veheria Avatar answered Nov 01 '22 11:11

Oleh Veheria