The code below appears to give bad dimensions for a brief period, and then gives the correct ones. This was not happening prior to 16.4.1, it just started happening. The print statement will show a height and width of 0, and then it prints again, showing the correct height/width.
This code is much simpler than my real code, in my real code the width was something like 73, and the height was a tiny number (something like 10^(-15)). But again, it was just for a moment, but long enough to crash the app (given all the computations around height/width).
Now, I protected the code with something like:
if (height > 1.0 && width > 1.0) { ... }
But I feel this is flaky. Who knows how things are going to change in the future.
First question, why do I get bad dimensions for an instant? I'm guessing it has something to do with the sheet taking its time to come up?
Second question, is there a better way to check for bad dimensions, or readiness of the sheet?
import SwiftUI
struct ContentView: View {
@State private var showingSheet = false
var body: some View {
HStack {
Text("Click to show sheet")
Spacer()
Button {
showingSheet = true
} label: {
Image(systemName: "chevron.down")
}
.padding()
.sheet(isPresented: $showingSheet) {
SheetView(showingSheet: $showingSheet)
}
}.frame(height: 30).onTapGesture { showingSheet = true }.padding()
}
}
struct SheetView: View {
@Binding var showingSheet: Bool
var body: some View {
VStack {
GeometryReader { reader in
let _ = print("READER width = \(reader.size.width) height = \(reader.size.height)")
Text("HELLO").padding()
}
}.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}