gshaw / notes

Issues and solutions I find during software development.
https://gshaw.ca
MIT License
1 stars 0 forks source link

Bug with sheet and Menu on same View #9

Open gshaw opened 1 year ago

gshaw commented 1 year ago

Environment: Xcode 14.2 Swift iOS 16.2 iPhone 12 mini. Must run on device, simulator works as expected.

Steps to reproduce

  1. Have a button that when tapped shows a sheet.
  2. Have a menu that when opened doesn't hide the button that shows the sheet
  3. Tap to open the menu
  4. Tap to open the sheet

Expected

The menu to close and the sheet to open.

Actual

The menu closes as expected but the sheet does not open even though the state is changed by the button. If the menu is not open this does not happen.


import SwiftUI

@main
struct SheetMenuBugApp: App {
    @State private var showSheet = false

    var body: some Scene {
        WindowGroup {
            VStack {
                Button("Open Sheet") { showSheet = true }
                Text("showSheet: \(showSheet ? "T" : "F")")
                Spacer()
                Text("Open Sheet doesn't work if you show the menu and tap Open Sheet.")
                Spacer()
                Menu(
                    content: { Text("Empty Menu") },
                    label: { Text("Show Menu") }
                )
            }
            .padding()
            .sheet(isPresented: $showSheet) { Text("Sheet Contents") }
        }
    }
}

SheetMenuBug.zip


https://user-images.githubusercontent.com/33321/228053724-4a0f86a8-637a-4002-bd34-b6e7884aa8e3.MP4

gshaw commented 1 year ago

Work around is to perform the state change after sleeping for a short duration.

Button("Open Sheet") {
    Task {
        try? await Task.sleep(nanoseconds: 1_000_000) // 1ms
        showSheet = true
    }
}
gshaw commented 1 year ago

Reported to Apple: https://feedbackassistant.apple.com/feedback/12082620