I have a simple view in SwiftUI, Scrap3.swift.
The view is composed of two structs, ColorSquare (simple struct that creates a view with different colored squares & text – takes color and text as arguments) and Scrap3 -> this returns the main view.
The main view is simply a button that toggles the state var that triggers the popover. While the code to toggle the popover from the main view works fine, the issue is once the popover is opened. After a minute or so, the popover is automatically dismissed despite not interacting with the view. I would expect the popover to remain open indefinitely until a user taps the Dismiss button at the button of the popover.
After a lot of searching through those with similar issues in the past I added let _ = print(Self._printChanges()) in an attempt to discover what was changing in the body to trigger the view refreshing (i.e. popover being dismissed).
Looking at the console output unfortunately did not provide me any actionable insight, as it’s just this message associated with popover being dismissed displaying every 30 – 90 seconds:
Scrap3: @self, @identity, _showingPopover, _dismiss changed.
()
Mind you, there was zero interaction to cause the popover to be dismissed during this time.
I’m at a loss.
Here is the full code for the Scrap3.swift file:
import SwiftUI
struct ColorSquare: View {
let color: Color
let label: String
var body: some View {
ZStack {
color
.frame(width: 200, height: 75, alignment: .center)
HStack {
Spacer()
Text(label)
.padding(.horizontal, 10)
.frame(maxWidth: .infinity)
.padding(.vertical, 4)
.padding(.horizontal, 8)
.foregroundColor(.white)
.font(.system(size: 30))
}
}
}
}
struct Scrap3: View {
@State private var showingPopover = false
@Environment(\.dismiss) var dismiss
var body: some View {
let _ = print(Self._printChanges()) //Debug attempt to see which property is changing to force the view to redraw
VStack {
Button("Show Menu") {
showingPopover = true
}
}
.popover(isPresented: $showingPopover) {
Text("two by seven table")
.font(.system(size: 36))
.bold()
.padding(10)
Grid(alignment: .center, horizontalSpacing: 1, verticalSpacing: 1) {
GridRow {
Text("Letter")
.font(.system(size: 22))
Text("Number")
.font(.system(size: 22))
}
.bold()
Divider()
GridRow {
ColorSquare(color: .red, label: "1")
ColorSquare(color: .red, label: "A")
}
Divider()
GridRow {
ColorSquare(color: .orange, label: "2")
ColorSquare(color: .orange, label: "B")
}
Divider()
GridRow {
ColorSquare(color: .yellow, label: "3")
ColorSquare(color: .yellow, label: "C")
}
Divider()
GridRow {
ColorSquare(color: .green, label: "4")
ColorSquare(color: .green, label: "D")
}
Divider()
GridRow {
ColorSquare(color: .indigo, label: "5")
ColorSquare(color: .indigo, label: "E")
}
Divider()
GridRow {
ColorSquare(color: .purple, label: "6")
ColorSquare(color: .purple, label: "F")
}
Divider()
GridRow {
ColorSquare(color: .pink, label: "7")
ColorSquare(color: .pink, label: "G")
}
}
.presentationCompactAdaptation(.fullScreenCover)
Spacer()
Button("Dismiss") {
showingPopover = false
}
}
} //END: Body
}
#Preview {
Scrap3()
}




