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