Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS11 SearchController - SearchBar removal from navigationItem leaves broken UI

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

like image 307
Rakesha Shastri Avatar asked Oct 02 '17 20:10

Rakesha Shastri


2 Answers

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.

like image 176
Sergey Gamayunov Avatar answered Nov 17 '22 15:11

Sergey Gamayunov


Use this :

let search = UISearchController(searchResultsController: nil)

If you are setting the below, then remove this line

self.navigationItem.searchController = search
like image 3
Vini App Avatar answered Nov 17 '22 14:11

Vini App