iOS 11 prefersLargeTitles not expanding after orientation change
Asked Answered
A

3

9

I've implemented the iOS 11 feature prefersLargeTitles and it works just fine. Portrait mode is working as expected:

enter image description here

I understand the large title will always stay collapsed (small) in landscape mode and that's fine to me. The problem is when I try to change to landscape and then again to portrait, the large title should be expanded (big) by default back in portrait mode, but it won't until I scroll down a bit:

enter image description here

My code looks quite simple:

if #available(iOS 11.0, *) {
  navigationController?.navigationBar.prefersLargeTitles = true
  navigationItem.largeTitleDisplayMode = .always
}

I also tried using different values on tableView.contentInsetAdjustmentBehavior, nothing changed. I'm kind of solving it by now scrolling down the table programmatically after orientation changes, but I think that's just a (not very nice) workaround.

Is that supposed to be working as expected? Is it something left in my implementation? Is there a better workaround to this?

Abruption answered 24/8, 2018 at 11:54 Comment(4)
Hello, did you find a solution. I'm having the same problem, when switching large titles like so: self.navigationController?.navigationBar.prefersLargeTitles = !(self.navigationController?.navigationBar.prefersLargeTitles)!Pasta
Hi. Unfortunately I didn't. I'm still using a workaround after orientation changes.Abruption
Hey! I'm also having the same problem. Can you publish your workaround by answering your own question?Heraclea
Did any of you find a solution to this issue? Is this always the case? I have my navigation controller inside a split view controller. Could that be the culprit?Contented
N
6

I faced the same issue. This worked for me.

    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        navigationItem.largeTitleDisplayMode = .always
        coordinator.animate(alongsideTransition: { (_) in
            self.coordinator?.navigationController.navigationBar.sizeToFit()
        }, completion: nil)
    }
Nearsighted answered 21/8, 2019 at 20:12 Comment(3)
Explain this briefly.Radiothorium
Sure, since I have my navigationItem to have an always the largeTitleDisplayMode , it will try to set the large title on the orientation change via sizeToFit(). I'm using a coordinator pattern for my VC's hence the self.coordinator? but the sizeToFit on the navigationBar should resize the navigationBar to show the large title if it can.Nearsighted
@Nearsighted However, we can't able to retain the scroll position, when we do sizeToFit.Cementite
H
0

One approach could be save the maximum navigation bar height, and set it during rotation.

Something like this:


var maximumHeight: CGFloat = 0

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {

        super.viewWillTransition(to: size, with: coordinator)

        guard let navigationController = navigationController else {
            return
        }

        if maximumHeight < navigationController.navigationBar.frame.height {
            maximumHeight = navigationController.navigationBar.frame.height
        }

        coordinator.animate(alongsideTransition: { (_) in

            navigationController.navigationBar.frame.size.height = self.maximumHeight

        }, completion: nil)
}

In landscape, the system knows that it must change its size, so you don't have to worry about it.

@rassar @twofish

Helli answered 4/8, 2019 at 20:33 Comment(0)
H
0

iOS 16

First set the PrefersLargeTitles to true either on ViewDidLoad() or on the NavigationBar in the storyboard.

Then on the Navigation Controller's rootController add this:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)
    
    coordinator.animate { (_) in
        self.navigationController?.navigationBar.sizeToFit()
    }
}
Hagiocracy answered 24/1, 2023 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.