Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Navigation Bar back button disappears after showing an alert

I have a simple view:

struct AccountView: View {
    @State private var showingLogoutAlert = false
    var body: some View {
        Button("Log out!") {
            showingLogoutAlert = true
        }
        .alert(isPresented: $showingLogoutAlert, content: {
            Alert(title: Text("Log out of Flow?"),
                  primaryButton: .cancel(),
                  secondaryButton: .destructive(Text("Log out"), action: {
                //logOut()
            }))
        })
        
    }
}

When you tap the button, it will show an alert. The problem is, after the alert dismisses, the back button also disappears! I have no idea why this is happening. See the gif below.

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’ve tried to reduce the problem down to the bare minimum code. Replicated it on a separate, new app. Still the same issue.

>Solution :

this is the working code I used in my test, on MacOS 13.2, Xcode 14.2, tested on real ios 16.3 devices (not Previews), and macCatalyst.

struct ContentView: View {
    var body: some View {
        NavigationStack {
            NavigationLink(destination: AccountView()) {
                Text("AccountView")
            }
        }
    }
}

struct AccountView: View {
    @State private var showingLogoutAlert = false
    var body: some View {
        Button("Log out!") {
            showingLogoutAlert = true
        }
        .alert(isPresented: $showingLogoutAlert, content: {
            Alert(title: Text("Log out of Flow?"),
                  primaryButton: .cancel(),
                  secondaryButton: .destructive(Text("Log out"), action: {
                //logOut()
            }))
        })
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading