SwiftUI Pickers crash Watch app

I'm still just feeling out SwiftUI, so maybe someone can tell me if I'm missing something obvious.


My Pickers on Apple Watch always seem to crash my app when scrolling all the way up to the first option, then attempting to scroll down even a frame. The console output seems familiar from pre-SwiftUI:


*** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan nan].

I've reduced this to the simplest SwiftUI Picker I can imagine, and it still crashes.


struct TestPicker: View {
  @State var test: Int = 1
  var body: some View {
    Picker("Pick!", selection: $test) {
      ForEach(1...3, id: \.self) { value in
        Text(String(value))
      }
    }
  }
}

Accepted Reply

All right – inferring the crash had something to do with view geometry, I've at least prevented the crash by adding a `.fixedSize()` modifier to the dynamic `Text()` views. I still only have the vaguest idea why that would help, or why a crash occurred in the first place.

Replies

I have not practiced SwiftUI a lot yet.


But the SwiftUI examples I saw are calling a bit differently, more like :

Picker(selection: $test, label:"Pick!") { 
      ForEach(1...3, id: \.self) { value in 
     Text(String(value)) 
 }

It's really hard to tell with the minimal documentation – it seems like these are just two similar APIs. I've just tried your pattern and it works but produces the same crash. Good thought, though.

All right – inferring the crash had something to do with view geometry, I've at least prevented the crash by adding a `.fixedSize()` modifier to the dynamic `Text()` views. I still only have the vaguest idea why that would help, or why a crash occurred in the first place.