3

I want a large title in the navigationbar on a pushed view in SwiftUI and an inline title on the parent view.

When the parent navigation bar display mode is not set, it works: Working without display mode on parent

But when I set the display mode in the parent view to inline, the title on the second screen is inline, instead of large. You can drag the list and the title will stay large. (You can see a small example in the code below)

With display mode to inline on the parent, the child is also inline.

Here is a small example:

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: DestinationView()) {
                Text("Next Screen")
            }
            .navigationBarTitle("Start screen", displayMode: .inline)
        }
    }
}

struct DestinationView: View {
    var body: some View {
        ScrollView {
            VStack{
                ForEach((1...10), id: \.self) {
                    Text("\($0)")
                }
            }
        }
        .navigationBarTitle("Second screen", displayMode: .large)
    }
}

There are several post with similar questions: https://www.reddit.com/r/iOSProgramming/comments/g2knmp/large_title_collapses_after_a_push_segue/ -> Same problem but with UIKit and we don't have prefersLargeTitles in SwiftUI.

Large title doesn't appear large -> Same problem with UIKit and marked as answered with preferesLargeTitles.

Navigation bar title stays inline in iOS 15 -> Here was a fix from apple side, but it was a back navigation

thekakami
  • 31
  • 4
  • Instead of using display mode, try actually setting `.navigationBarTitleDisplayMode(.large)` in the second view, or when you call the second view from the first view. –  May 23 '22 at 14:11
  • I did it before posting my code here. So `.navigationTitle(Text("Second screen")) .navigationBarTitleDisplayMode(.large)` is the same like my code. – thekakami May 24 '22 at 06:59
  • What do you mean with "or when call the second view". The SwiftUI way is the NavigationLink. In UIKit I could implement the segue call or anything else, but how it is possible in SwiftUI? – thekakami May 24 '22 at 07:03
  • I was suggesting something like this: `NavigationLink("Next Screen"){DestinationView().navigationBarTitleDisplayMode(.large)}`. –  May 24 '22 at 14:08
  • It doesn't work either. It's my first big SwiftUI project after more than 5 years of UIKit and it's frustrating. At the weekend I will write a radar or feedback or something. – thekakami May 25 '22 at 12:04
  • You may have to wait for WWDC. It appears that once you set the display mode, you can't go back within the same `NavigationView` hierarchy. Otherwise, you can file a Radar. – Yrb May 25 '22 at 12:10
  • I had a similar experience. What helped is to realize that the Simulator only does the error behaviour - however on the real device it worked... – iKK Nov 02 '22 at 10:36

1 Answers1

0

just place .navigationViewStyle(.stack) in NavigationView in ContentView()

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 24 '22 at 23:25