Viktor Maric
Viktor Maric

Reputation: 603

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

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()
            ])
        }

}

}

Upvotes: 10

Views: 2399

Answers (2)

notduongdang
notduongdang

Reputation: 41

Add

.navigationViewStyle(StackNavigationViewStyle())

to the NavigationView seems to fix it for me.

Upvotes: 0

Viktor Maric
Viktor Maric

Reputation: 603

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()
        ])
    }
}
}

Upvotes: 1

Related Questions