Top Safe Area Constraint Animation
Asked Answered
F

2

5

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

Fluoroscopy answered 15/8, 2018 at 14:13 Comment(0)
L
7

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()
    }
}
Lecroy answered 15/8, 2018 at 14:31 Comment(2)
Thnak you sir, you've made my dayEnamelware
Worth noting that in this case, the duration does not seem to be taken into account and seems to be using the presentation animation's durationEnamelware
M
2

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.

Maggard answered 23/12, 2019 at 6:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.