Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Top Safe Area Constraint Animation

Setup

  1. A simple view controller with a UISearchController set in the navigation item to use iOS 11's search functionality in the search bar.

  2. Any view with it's top constrained to the SafeArea.Top

Problem

When presenting the search controller, the navigation bar is animated because of it's size change, but the constraint to top area does not follow the animation.

If anyone have an idea of what I can do (right now I guess my only choice is to disable the hidesNavigationBarDuringPresentation to avoid the animation at all)

See example below where I activated the slow animations for easier understanding:

enter image description here

like image 687
Marc-Alexandre Bérubé Avatar asked Jan 27 '23 14:01

Marc-Alexandre Bérubé


2 Answers

You could animate the constraint change with UIView.animate. Since your constraint is based on the view's safe area, the viewSafeAreaInsetsDidChange method could alert you of changes in the constraint value :

override func viewSafeAreaInsetsDidChange() {
    UIView.animate(withDuration: 1) {
        self.view.layoutIfNeeded()
    }
}
like image 103
wheeliez Avatar answered Mar 16 '23 03:03

wheeliez


I faced the same issue on iOS 13.3. This code fixed it.

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        navigationController?.view.backgroundColor = .white
        navigationController?.navigationBar.isTranslucent = false
    }

If you disable isTranslucent on viewDidLoad, searchBar would be hidden when the view appears. You can also utilize navigationItem.hideSearchBarWhenScrolling to avoid putting the code in viewDidAppear.

like image 38
Shingo Fukuyama Avatar answered Mar 16 '23 02:03

Shingo Fukuyama