Toolbar buttons become unusable after closing the sheet.

When I run the following code, once I open and close the sheet, the toolbar buttons become unusable. When there is only one button, there is no problem, but when there are two buttons, this phenomenon occurs. Is there a workaround?

The environment is Xcode: 12.5, iOS: 14.5.

import SwiftUI

struct ContentView: View {
  @State private var showingSheet = false
   
  var body: some View {
    NavigationView {
      Text("1st View")
        .toolbar {
          ToolbarItem(placement: .navigationBarTrailing) {
            HStack {
              Button("Button1") {
                showingSheet = true
              }
              Button("Button2") {
                showingSheet = true
              }
            }
          }
        }
        .sheet(isPresented: $showingSheet) {
          Sheet()
        }
    }
  }
}

struct Sheet: View {
  @Environment(\.presentationMode) var presentationMode
  var body: some View {
    Button("close sheet") {
      presentationMode.wrappedValue.dismiss()
    }
  }
}
  • your code as it is works well for me on macos 11.4, xcode 12.5, target ios 14.5 and macCatalyst 11.3.

  • Thank you for the information. I tried it on a real machine and it works as you said. It seems to be a problem with the simulator.

Add a Comment

Accepted Reply

Can you try something like this?

struct ContentView: View {
    @State private var showingSheet = false
    
    var body: some View {
        NavigationView {
            Text("1st View")
                .toolbar {
                    ToolbarItemGroup(placement: .navigationBarTrailing) {
                        Button("Button1") {
                            showingSheet = true
                        }
                        Button("Button2") {
                            showingSheet = true
                        }
                    }
                }
                .sheet(isPresented: $showingSheet) {
                    Sheet()
                }
        }
    }
}

Or this:

struct ContentView2: View {
    @State private var showingSheet = false
    
    var body: some View {
        NavigationView {
            Text("1st View")
                .toolbar {
                    ToolbarItem(placement: .navigationBarTrailing) {
                        Button("Button1") {
                            showingSheet = true
                        }
                    }
                    ToolbarItem(placement: .navigationBarTrailing) {
                        Button("Button2") {
                            showingSheet = true
                        }
                    }
                }
                .sheet(isPresented: $showingSheet) {
                    Sheet()
                }
        }
    }
}
  • Thank you. I was able to confirm that both work. The reason it doesn't work with HStack seems to be a problem with the simulator.

Add a Comment

Replies

Can you try something like this?

struct ContentView: View {
    @State private var showingSheet = false
    
    var body: some View {
        NavigationView {
            Text("1st View")
                .toolbar {
                    ToolbarItemGroup(placement: .navigationBarTrailing) {
                        Button("Button1") {
                            showingSheet = true
                        }
                        Button("Button2") {
                            showingSheet = true
                        }
                    }
                }
                .sheet(isPresented: $showingSheet) {
                    Sheet()
                }
        }
    }
}

Or this:

struct ContentView2: View {
    @State private var showingSheet = false
    
    var body: some View {
        NavigationView {
            Text("1st View")
                .toolbar {
                    ToolbarItem(placement: .navigationBarTrailing) {
                        Button("Button1") {
                            showingSheet = true
                        }
                    }
                    ToolbarItem(placement: .navigationBarTrailing) {
                        Button("Button2") {
                            showingSheet = true
                        }
                    }
                }
                .sheet(isPresented: $showingSheet) {
                    Sheet()
                }
        }
    }
}
  • Thank you. I was able to confirm that both work. The reason it doesn't work with HStack seems to be a problem with the simulator.

Add a Comment