Child NavigationView doesn't honor modifiers on iOS 15

Viewed 112

I have an issue with a child NavigationView that doen't honor modifiers like you can try and see with below single code file working app:

import SwiftUI

@main
struct NavigationView_IssueApp: App {
    @StateObject var viewState = ViewState()
    
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(viewState)
        }
    }
}


struct ContentView: View {
    @EnvironmentObject var viewState: ViewState
    var body: some View {
        ZStack {
            Button("Tap Me") {
                viewState.showAllCards.toggle()
            }
            if !viewState.showAllCards {
                ChildNavigationView()
            }
        }
    }
}

struct ChildNavigationView: View {
    @EnvironmentObject var viewState: ViewState
    
    var body: some View {
        NavigationView {
            CardDetailView()
                .navigationBarTitleDisplayMode(.inline)
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

struct CardDetailView: View {
    @EnvironmentObject var viewState: ViewState
    
    var body: some View {
        Color.yellow
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button(action: { viewState.showAllCards.toggle() }) {
                        Text("Done")
                    }
                }
            }
    }
}

class ViewState: ObservableObject {
    init() {
        print("init ViewState")
    }
    @Published var showAllCards = true
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .environmentObject(ViewState())
    }
}

In Canvas Preview looks good.

enter image description here

On a iOS 14 device looks good.

enter image description here

On a iOS 15 device doesn't honor modifiers.

enter image description here

Does anyone have a clue about that strange behaviour?

1 Answers

Looks like a bug. Here is found safe workaround

struct ChildNavigationView: View {
    @EnvironmentObject var viewState: ViewState
    @State private var mode = NavigationBarItem.TitleDisplayMode.automatic
    var body: some View {
        NavigationView {
            CardDetailView()
                .navigationBarTitleDisplayMode(mode)
        }
        .navigationViewStyle(StackNavigationViewStyle())
        .onAppear {
            mode = .inline  // << switch mode right after construction
        }
    }
}
Related