Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 14 large titles not working properly when navigating between views [duplicate]

I have a ViewController and a DetailViewController, in the ViewDidLoad of the ViewController I set the following code, the purpose is to make the ViewController always use the large title

self.navigationController?.navigationBar.prefersLargeTitles = true
navigationItem.largeTitleDisplayMode = .always

In the ViewDidLoad of the DetailViewController I set the following code, the purpose is to make the DetailViewController not use the large title

navigationItem.largeTitleDisplayMode = .never

When I return from DetailViewController to ViewController, the small title is displayed instead of the large title in ViewController. This code is correct in iOS12 and iOS13. How to make the ViewController always display the large title on iOS14?

Currently using Xcode12 from the App Store

like image 987
BaQiWL Avatar asked Nov 16 '22 05:11

BaQiWL


2 Answers

For iOS 14, need to add sizeToFit function. Below code always work.

navigationController?.navigationBar.prefersLargeTitles = true
navigationController?.navigationBar.sizeToFit()
like image 100
Symon Avatar answered Mar 30 '23 00:03

Symon


At last solved the issue.

Edge case:

If you're using large title, and you have multiple scroll views on the same view controller. Navigation bar will listen to scroll actions on the subviews (immediate subview) of kind UIScrollView.

Solution

You have to prevent current view controller's large title collapsing feature.

Its same concept as @BaQiWL mentioned. But not just adding view.addSubview(UIView()) will solve this issue, if you're using storyboard.

To do this you have to add view as Viewcontroller's first subview. (view.sendSubviewToBack does the trick).

// Call this method on `viewDidLoad`
private func preventLargeTitleCollapsing() {
    let dummyView = UIView()
    view.addSubview(dummyView)
    view.sendSubviewToBack(dummyView)
}

OR via Storyboard:

enter image description here

like image 37
Lal Krishna Avatar answered Mar 30 '23 00:03

Lal Krishna