UISearchController Not Redisplaying Navigation Bar on Rotate

后端 未结 5 1929
醉话见心
醉话见心 2020-12-08 23:10

I have implemented the UISearchController and it is working great except...

When I click on the Search Bar the Navigation Bar disappears nicely as expected. When I r

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-09 00:00

    I've faced a similar issue with UISearchController using it with navigationItem. It looks differently, but caused by the very same problem: UISearchController does NOT update searchBar frame when status bar reappears.

    But instead of manually adjusting frames of searchBar's view hierarchy you should simply force it to update layout by toggling hidesNavigationBarDuringPresentation back and forth during rotation either in traitCollectionDidChange or viewWillTransition(to size: CGSize, with cooridnator: UIViewControllerTransitionCoordinator):

    So the fix would look like this:

     override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        if #available(iOS 11, *) {
            navigationItem.hidesSearchBarWhenScrolling.toggle()
            navigationItem.hidesSearchBarWhenScrolling.toggle()
        }
    }
    

    And here is how the original issue looks like.

    Then rotating and got this:

提交回复
热议问题