Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UISearchController and NavigationBar back

Tags:

ios

swift

I'm addapting this code in my project https://github.com/stuartbreckenridge/UISearchControllerWithSwift

After Navigation Controller and before Countries Scene (ViewController) I have put one more View Controller, so now in Countries View controller I have back button. After inputing something in search bar when I press Back, search bar still is visible.

I've tried hide it doing this (countrySearchController is UISearchController):

override func viewWillDisappear(animated: Bool) {
    self.countrySearchController.active = false
}

But it hides UISearchController with delay, so it's stil visible for half a second.

Input something: enter image description here

And then press back: enter image description here

like image 352
user1483208 Avatar asked Mar 17 '26 22:03

user1483208


1 Answers

Correct way probably is to set self.definesPresentationContext in viewDidLoad

self.definesPresentationContext = true

That caused some glitches when UISearchController became active, so I just removed the searchBar from Superview.

override func viewWillDisappear(animated: Bool) {
    searchController.searchBar.removeFromSuperview()
    searchController.active = false;
    super.viewWillDisappear(true)
}
like image 52
David Němec Avatar answered Mar 20 '26 13:03

David Němec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!