0

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()
    }
}

danronmoon
  • 3,814
  • 5
  • 34
  • 56
Jack
  • 2,206
  • 3
  • 18
  • 25
  • Look into SwiftUI.Layout it is likely a better option. – lorem ipsum Apr 29 '23 at 17:56
  • Please give us a link to the change log for 16.4.1 and tell us which item (if there is one) applies to this. – Andrew Morton Apr 29 '23 at 18:06
  • @AndrewMorton I'm not sure what you mean. I'm not privy to whatever changes apple made with 16.4.1. I can say that the print statement in the above code only executes once in previous versions, and with 16.4.1 it's twice, as above. – Jack Apr 30 '23 at 17:21

0 Answers0