Search bar not showing up on the navigation bar iOS 11
Asked Answered
C

2

8

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()
}
Condescendence answered 4/7, 2018 at 15:53 Comment(8)
I used your code it is workingDevilfish
How you are pushing new view controller?Decaliter
I am using this - let destinationVC = storyboard.instantiateViewController(withIdentifier:"someVC") vc.show(destinationVC, sender: self)Condescendence
This got solved by adding this line - navigationItem.hidesSearchBarWhenScrolling = falseCondescendence
Now the problem is the search bar is coming below the navigationBar. How can we have searchbar on top of navigation bar?Condescendence
I dont think you can have it above the navigation bar unless you create your ownCavernous
You can hide the navigation bar whilst searching using searchController.hidesNavigationBarDuringPresentation = trueCavernous
Did you find the solution? @AjayKumarAlsatian
B
14

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.

Browband answered 22/10, 2018 at 8:54 Comment(0)
K
0

So, navigationItem.hidesSearchWhenScrolling only works when you set the searchController property of navigationItem NOT when you set the navigationItem.titleView to searchBar.

Kurrajong answered 4/6, 2019 at 22:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.