iOS Development

ios – SwiftUI popover dismisses itself routinely regardless of not having something power the view to refresh?

Spread the love


I’ve a easy view in SwiftUI, Scrap3.swift.

The view consists of two structs, ColorSquare (easy struct that creates a view with completely different coloured squares & textual content – takes shade and textual content as arguments) and Scrap3 -> this returns the primary view.

The primary view is solely a button that toggles the state var that triggers the popover. Whereas the code to toggle the popover from the primary view works wonderful, the difficulty is as soon as the popover is opened. After a minute or so, the popover is routinely dismissed regardless of not interacting with the view. I’d anticipate the popover to stay open indefinitely till a person faucets the Dismiss button on the button of the popover.

After quite a lot of looking out by these with related points previously I added let _ = print(Self._printChanges()) in an try to find what was altering within the physique to set off the view refreshing (i.e. popover being dismissed).

Trying on the console output sadly didn’t present me any actionable perception, because it’s simply this message related to popover being dismissed displaying each 30 – 90 seconds:

Scrap3: @self, @id, _showingPopover, _dismiss modified.
()

Thoughts you, there was zero interplay to trigger the popover to be dismissed throughout this time.

I am at a loss.

Right here is the total code for the Scrap3.swift file:

import SwiftUI

struct ColorSquare: View {
    let shade: Shade
    let label: String
    
    var physique: some View {
        
            ZStack {
                shade
                    .body(width: 200, peak: 75, alignment: .heart)
                HStack {
                   Spacer()
                    Textual content(label)
                        .padding(.horizontal, 10)
                        .body(maxWidth: .infinity)
                        .padding(.vertical, 4)
                        .padding(.horizontal, 8)
                        .foregroundColor(.white)
                        .font(.system(dimension: 30))
                }
            }
    }
}


struct Scrap3: View {

@State non-public var showingPopover = false
@Atmosphere(.dismiss) var dismiss
    

    var physique: some View {
        let _ = print(Self._printChanges()) //Debug try to see which property is altering to power the view to redraw
        VStack {
            Button("Present Menu") {
                showingPopover = true
            }
        }
        .popover(isPresented: $showingPopover) {
            Textual content("two by seven desk")
                .font(.system(dimension: 36))
                .daring()
                .padding(10)
            Grid(alignment: .heart, horizontalSpacing: 1, verticalSpacing: 1) {
                GridRow {
                    Textual content("Letter")
                        .font(.system(dimension: 22))
                    Textual content("Quantity")
                        .font(.system(dimension: 22))
                }
                .daring()
                
                Divider()
                GridRow {
                    ColorSquare(shade: .crimson, label: "1")
                    ColorSquare(shade: .crimson, label: "A")
                }
                Divider()
                GridRow {
                    ColorSquare(shade: .orange, label: "2")
                    ColorSquare(shade: .orange, label: "B")
                }
                Divider()
                GridRow {
                    ColorSquare(shade: .yellow, label: "3")
                    ColorSquare(shade: .yellow, label: "C")
                }
                Divider()
                GridRow {
                    ColorSquare(shade: .inexperienced, label: "4")
                    ColorSquare(shade: .inexperienced, label: "D")
                }
                Divider()
                GridRow {
                    ColorSquare(shade: .indigo, label: "5")
                    ColorSquare(shade: .indigo, label: "E")
                }
                Divider()
                GridRow {
                    ColorSquare(shade: .purple, label: "6")
                    ColorSquare(shade: .purple, label: "F")
                }
                Divider()
                GridRow {
                    ColorSquare(shade: .pink, label: "7")
                    ColorSquare(shade: .pink, label: "G")
                }
                
            }
                .presentationCompactAdaptation(.fullScreenCover)
            Spacer()
            Button("Dismiss") {
                showingPopover = false
            }
        }
    } //END: Physique
}

#Preview {
    Scrap3()
}

2 thoughts on “ios – SwiftUI popover dismisses itself routinely regardless of not having something power the view to refresh?

Leave a Reply

Your email address will not be published. Required fields are marked *