TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Unexpected Behaviour with .toolbar and Navigation bar

Forums > SwiftUI

I have come across this when putting a Button in the Navigation bar with an alert the "back" button disapear and get warning on loading last screen (with or without button) of

[Assert] displayModeButtonItem is internally managed and not exposed for DoubleColumn style. Returning an empty, disconnected UIBarButtonItem to fulfill the non-null contract.

This is a small code the replicates the problem.

Does anyone know want wrong and how to fix it.

struct ContentView: View {
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: SecondView()) {
                        Text("Go To Second View")
                }
            }
            .navigationBarTitle("First View", displayMode: .inline)
        }
    }
}

struct SecondView: View {
    var body: some View {
        VStack {
            NavigationLink(destination: ThirdView()) {
                Text("Go to Third View")
            }
        }
        .navigationTitle("Second View")
    }
}

struct ThirdView: View {
    @State private var showingAlert = false

    var body: some View {
        VStack {
            Text("Last View")
        }
        .navigationTitle("Third View")
        .toolbar {
            Button {
                showingAlert.toggle()
            } label: {
                Label("Send Mail", systemImage: "tray.and.arrow.up.fill")
            }
        }
        .alert(isPresented: $showingAlert) {
            Alert(title: Text("Alert"), message: Text("alert message"), dismissButton: .default(Text("OK")))
        }
    }
}

3      

I have solved my own question. FYI but puttin .navigationViewStyle(StackNavigationViewStyle()) on the NavigationView solves the problem

struct ContentView: View {
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(destination: SecondView()) {
                        Text("Go To Second View")
                }
            }
            .navigationBarTitle("First View", displayMode: .inline)
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

5      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.