Search code examples
swiftuinavigationcontrolleruinavigationbaruisearchbarinteractivepopgesture

Navigation item views disappearing when using multiple search bars and interactive pop gesture


I'm using multiple UISearchBar's as titleViews in UINavigationControllers embedded in a UITabBarController. This all works just fine when I push and pop view controllers using the back button. But, the search bar starts to disappear when I pop using the interactive pop gesture.

I'm not sure how to fix this.

I've tried setting the titleView of the navigationItem in viewWillAppear and viewDidAppear but both didn't work.

The search bar accepts input and touches, it's just not visible.

Here's the viewWillAppear code:

override func viewWillAppear(_ animated: Bool) {
    print("view will appear from base search")
    super.viewWillAppear(animated)
    print(self.searchController.searchBar)
    self.definesPresentationContext = true
    self.navigationItem.titleView = nil
    self.navigationItem.titleView = self.searchController.searchBar
    self.navigationController?.setNavigationBarHidden(false, animated: true)
}

Here are some photos of what happens:

https://i.sstatic.net/E6Nh9.jpg

The first photo is the homepage.

The second photo is a user typing in the search query.

The third photo is when a user returns after executing interactive pop gesture.

The fourth photo shows that you can still type in the search bar even though it's not visible.

The fifth photo shows you can still hit the cancel button next to the search bar even though that's not visible.


Solution

  • I fixed this by setting: self.navigationItem.searchController = searchController

    instead of setting:

    self.navigationItem.titleView = searchController.searchBar