Toolbar issue with ActionSheet

Viewed 275

I have the following example code:

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink(destination: DetailViewToolbarBug()) {
                Text("Unknown")
            }
        }
    }
}
struct DetailViewToolbarBug: View {
    @State private var showActionSheet = false
    
    var body: some View {
        VStack {
            NavigationLink(destination: DetailViewToolbarBug()) {
                Text("Unknown")
            }
        }
        .actionSheet(isPresented: $showActionSheet) {
            ActionSheet(title: Text("Optionen"),
                buttons: [
                    .default(Text("Add"), action: {
                    })  
            ])
        }
        .navigationBarTitle("Detail")
        .toolbar {
            ToolbarItem(placement: .primaryAction) {
                
                Button(action: {
                    showActionSheet = true
                }) {
                    Image(systemName: "ellipsis.circle")
                }
            }
        }
        
    }
}

The issue: If you navigate in two or more times and then open the action sheet, the back button (will have the name Detail in it) will disappear and not come back again.

How to solve this?

1 Answers

This looks like a SwiftUI bug and worth submitting feedback to Apple.

Temporary workaround is to use old .navigationBarItems. Tested with Xcode 12 / iOS 14.

  // ... other code
        .navigationBarTitle("Detail")
        .navigationBarItems(trailing:
                 Button(action: {
                      showActionSheet = true
                 }) {
                      Image(systemName: "ellipsis.circle")
                 }
        )
//        .toolbar {
//            ToolbarItem(placement: .primaryAction) {
//
//                Button(action: {
//                    showActionSheet = true
//                }) {
//                    Image(systemName: "ellipsis.circle")
//                }
//            }
//        }

Related