0

I have this NavigationController hat has Large Titles enabled for its NavigationBar. The root ViewController has a SearchController, and hidesSearchBarWhenScrolling is set to True in the ViewController's NavigationItem as I don't want the SearchBar to be always visible. The ViewController has a TableView and when you tap on one of its items a new instance of the same ViewController will be pushed onto the Navigation stack using a storyboard segue. However, when looking at the transition between the current and the new ViewController one can observe that the animation doesn't look right: As soon as the new ViewController is moved in the SearchBar becomes empty, just showing its background. When the new ViewController is finally fully visible, the SearchBar will go away without any animation.

This is how I add the SearchController (nothing fancy here):

class MyViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

        let searchController = UISearchController(searchResultsController: nil)
        navigationItem.searchController = searchController
        navigationItem.hidesSearchBarWhenScrolling = true
    }
}

And so it looks like when navigating from "One" to "Two":

UISearchController / UINavigationBar shows broken animation when used within UINavigationController

enter image description here

Is there a way to make this look nicer? Of course, in the new ViewController the SearchBar should not be initially visible, so it has to go away somehow. But I would think that the SearchBar on the old ViewController perhaps should be faded out somehow instead of staying there and then suddenly hiding when the transition to the new ViewController is finished. Hopefully I'm just doing something wrong here...

Thanks and Merry Xmas to all of you,

Peter

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
PeterW
  • 1
  • 4

2 Answers2

0

Try setting the search controller to nil in the viewWillDissappear method.

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.navigationItem.searchController = nil
}
  • This actually seems to improve things a bit as now the expanded `SearchBar` in "One" will be moved off-screen as part of the transition to "Two". In "Two" the flickering that can be seen in my video is gone. I'm not sure though, if the `SearchBar` is intended to be animated that way. So I guess I will do some more testing going from there. – PeterW Dec 27 '18 at 16:26
0

Well, I finally found something very useful that I just couldn't find before asking my question:

Broken UISearchBar animation embedded in NavigationItem

Too bad this is known since iOS 11 and still not fixed.

PeterW
  • 1
  • 4