Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Responding to navigation bar height changes during push transition

In iOS 11 the search bar will now change the navigation bar height to 56dp when adding a search bar to the navigationItem.titleView

I like the height change and don't intend on forcing the height to stay at 44dp or lower.

unfortunately when transitioning from one view controller to another the pushed view will be drawn with the larger navigation bar in mind and then the bar height is changed after the transition is finished. That looks a little like this:

enter image description here enter image description here

I need a way of getting the navigation controller to recognise the height change during the transition so that it can animate to the smaller size and draw the view correctly.

I have one current fix which I don't like because it's a little jumpy and it's more work the app has to do and also it has to re-evaulate it's views regardless of which view controller it's being pushed from.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    if #available(iOS 11, *) {
        navigationController?.view.layoutSubviews()
    }
}

So far I haven't found any similar questions on stack overflow, any comments from WWDC and nothing in the official apple documentation. I've seen many apps deal with this however. The apple contacts app will create what looks like two navigation bars and will move between them without animating the height changes and the fb messenger app will perfectly transition between the heights and even allow for the interactive pop transition.

like image 903
David Rees Avatar asked Jan 24 '18 04:01

David Rees


People also ask

What happens if the navigation bar is too high or low?

Exceeding those values won’t hurt your device, but won’t have any impact on the navbar position either. A positive value would push the navigation bar up, while negative value would push it down (partially/fully hiding it even, of course).

Is-25 good to reduce the size of the navigation bar?

Our reader, Adam Bosworth, is finding the value of -25 good to reduce the size of the navbar. That’s it. Need any help changing the height of navigation bar on your device?

Can I change the color of the navigation bar?

While changing the color of the navigation bar is not a requirement to alter the height, we do recommend you do so. This should help you not only add a splash of color to the navigation menu, but help you easily visualize how much of the navigation bar is visible once you edit its position.

What is a navigation transition?

Navigational transitions occur when users move between screens, such as from a home screen to a detail screen. Navigation transitions use motion to guide users between two screens in your app. They help users orient themselves by expressing your app's hierarchy, using movement...


2 Answers

I tried a lot of different functions. This thing is the only one that really helps

OBJ C: if (@available(iOS 11, *)) { self.navigationController.view.layoutSubviews; }

SWIFT:

if #available(iOS 11, *) {
    self.navigationController.view.layoutSubviews()
 }
like image 157
Oleksandr Oleksyn Avatar answered Oct 15 '22 22:10

Oleksandr Oleksyn


Using this one, it will keep your searchbar's height fixed

if #available(iOS 11.0, *) {
        searchBar.heightAnchor.constraint(equalToConstant: 44.0).isActive = true
    }
like image 31
EricNguyen Avatar answered Oct 15 '22 22:10

EricNguyen