SwiftUI navigation bar title and items does not disappear when swiping back fails

Viewed 1794

enter image description here

The problem is that the title and the item of the navigation bar does not disappear which is an unexpected behaviour.

struct DestinationView: View {

@State private var showingActionSheet = false

var body: some View {
    Text("DestinationView")
        .padding(.top, 100)
        .navigationBarTitle(Text("Destination"), displayMode: .inline)
        .navigationBarItems(trailing: Button(action: {
            print("tapped")
        }, label: {
            Text("second")
        }))
        .actionSheet(isPresented: self.$showingActionSheet) { () -> ActionSheet in
            ActionSheet(title: Text("Settings"), message: nil, buttons: [
                .default(Text("Delete"), action: {
                }),
                .cancel()
            ])
        }

}

}
2 Answers

The problem is that the .navigationBarTitle(), .navigationBarItems() modifiers and the .actionSheet() modifier are under each other in code. (But it can be the .alert() or the .overlay() modifiers as well instead of .actionSheet())

The solution in this case:

struct DestinationView: View {

@State private var showingActionSheet = false

var body: some View {

    List {
        Text("DestinationView")
            .padding(.top, 100)
            .navigationBarTitle(Text("Destination"), displayMode: .inline)
            .navigationBarItems(trailing: Button(action: {
                print("tapped")
            }, label: {
                Text("second")
            }))
    }
    .actionSheet(isPresented: self.$showingActionSheet) { () -> ActionSheet in
        ActionSheet(title: Text("Settings"), message: nil, buttons: [
            .default(Text("Delete"), action: {
            }),
            .cancel()
        ])
    }
}
}

Add

.navigationViewStyle(StackNavigationViewStyle())

to the NavigationView seems to fix it for me.

Related