Popovers icon indicating copy to clipboard operation
Popovers copied to clipboard

How do I show popover at start?

Open stbdang opened this issue 2 years ago • 2 comments

Trying to show a popover as soon as a view appears.

@State var showingPopover: Bool = false

var body: some View {
    Button {
        showingPopover.toggle()
    } label: {
        Image(systemName: "questionmark.circle")
            .font(.title)
            .foregroundColor(.primary)
    }
    .popover(present: $showingPopover) {
        PopoverView()
    }
    .onAppear() {
        DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
            showingPopover = true
        }
    }
}

Setting showingPopover = true in onAppear without delay (asyncAfter) fails 100%

[Popovers] - No window was found when presenting popover. Please file a bug report (https://github.com/aheze/Popovers/issues).

I can kinda get it to work by adding delay but this isn't super reliable.

Any ideas?

stbdang avatar Jun 01 '22 20:06 stbdang

Yeah it takes a bit for the window to first become available. This is to support multiple windows on iPadOS... anyway, the best bet right now is probably just adding a 0.1 second delay.

aheze avatar Jun 07 '22 04:06 aheze

This happened to me as well. Can confirm that adding a slight delay (I used 0.2s) during onAppear as suggested works as expected. I agree with @stbdang that this isn't super reliable and it'd be nice to have a more deterministic way to ensure things work as expected.

richardpineo avatar Jun 08 '23 21:06 richardpineo