iOS 11 prefersLargeTitles not expanding after orientation change

Viewed 1221

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?

2 Answers

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)
    }

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

Related