Skip to content

Instantly share code, notes, and snippets.

@kalupas226
Last active November 22, 2022 01:24
Show Gist options
  • Save kalupas226/4de61802974e48464f4ecfc31bde5d93 to your computer and use it in GitHub Desktop.
Save kalupas226/4de61802974e48464f4ecfc31bde5d93 to your computer and use it in GitHub Desktop.
Popover (or sheet) is not working when Menu, Picker, etc is displayed.

FB11810368: Popover and sheet are not working when Menu, Picker, etc is displayed.

Environment: Xcode 14.1, iOS 16 (on iPad)

Popover (or sheet) is not working when Menu, Picker, etc is displayed.

struct ContentView: View {
    @State private var showPopover = false

    var body: some View {
        HStack {
            Menu {
                ForEach(0 ..< 8) { number in
                    Button {
                    } label: {
                        Text("\(number)")
                    }
                }
            } label: {
                Text("Menu")
            }
            Button {
                showPopover = true
            } label: {
                Text("Popover")
            }
            .popover(isPresented: $showPopover) {
                Text("hello")
            }
        }
    }
}

If I run the application with the above code and try to show the popover when the menu is open, I will get the error [Presentation] Attempt to present and will never be able to show the popover again. I have found a way to delay processing of present as a workaround, but it is not ideal.
For example,

struct ContentView: View {
    @State private var showPopover = false

    var body: some View {
        HStack {
            Menu {
                ForEach(0 ..< 8) { number in
                    Button {
                    } label: {
                        Text("\(number)")
                    }
                }
            } label: {
                Text("Menu")
            }
            Button {
                Task {
                    try await Task.sleep(nanoseconds: 300_000_000)
                    showPopover = true
                }
            } label: {
                Text("Popover")
            }
            .popover(isPresented: $showPopover) {
                Text("hello")
            }
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment