iOS11 SearchController - SearchBar removal from navigationItem leaves broken UI
Asked Answered
B

2

7

When i remove the searchController from navigationItem by setting 'nil'. Empty space is left behind where it used to be instead of collapsing.

Tried calling,

searchController.dismiss()
navigationController.navigationItem.searchController.dismiss()
navigationItem.searchController.dismiss()
searchController.isActive = false

Nothing worked.


P.S - Using simulator

Bebel answered 2/10, 2017 at 20:4 Comment(0)
G
14

You should layout subviews after removing search controller. The trick is which superview's subview you have to layout: since navigationItem is part of navigation stack, so you should call layoutSubviews() onto current navigationController:

navigationItem.searchController = nil
navigationController?.view.setNeedsLayout()
navigationController?.view.layoutIfNeeded()

According to Apple Documentation you shouldn't call layoutSubviews() directly.

Gymnasiarch answered 20/3, 2019 at 8:1 Comment(3)
This is working fine, to add it back just navigationItem.searchController = yourSearchControllerObjectSharonsharona
I'm having trouble getting this to work. It do notice my tableview/search content shifts up to the correct position, but the size of the navigation bar is still too large.Noun
saved my day! i was wrapping my head around this issue for hours. This is the only thing that worked for me!Austin
F
3

Use this :

let search = UISearchController(searchResultsController: nil)

If you are setting the below, then remove this line

self.navigationItem.searchController = search
Frequent answered 2/10, 2017 at 21:46 Comment(4)
The empty space still persists and if the searchbar is active it disappears along with the navigationbar.Bebel
That answer along with setting it to nil afterwards worked. Thanks. :DBebel
I had an issue where if I used the code above and I segued using the navController, no matter what I did the header for the nav would appear for the childVC. To solve that I used this line first. navigationItem.searchController?.hidesNavigationBarDuringPresentation = false I hope this solves and weird issues with VC's segueing you might get from the actions above.Carn
doing this seemed to work but i noticed the search bar still present in the view hierarchy. @SergeyGamayunov answer worked for meTutelary

© 2022 - 2024 — McMap. All rights reserved.