Does anyone know why this crashes, or could anyone tell me how to restructure this code so it doesn't crash. (this is FB11917078)
I have a view which displays two nested rectangles of a given aspect ratio (here 1:1). The inner rectangle is a fixed fraction of the outer rectangle's size.
When embedded in a List, if I rapidly resize the window, the app crashes. If the View is not in a List, there's no crash (and the requested aspect ratio is not respected, which I don't yet know how to fix).
Here's the code for the ContentView.swift file. Everything else is a standard macOS SwiftUI application template code from Xcode 14.2.
import SwiftUI
struct ContentView: View {
@State var zoomFactor = 1.2
var body: some View {
// rapid resizing of the window causes a crash,
// if the TwoRectanglesView is not embedded in a
// List, there is no crash
List {
ZStack {
Rectangle()
TwoRectanglesView(zoomFactor: $zoomFactor)
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct TwoRectanglesView: View {
@State private var fullViewWidth: CGFloat?
@Binding var zoomFactor: Double
private let aspectRatio = 1.0
var body: some View {
ZStack {
Rectangle()
.aspectRatio(aspectRatio, contentMode: .fit)
GeometryReader { geo in
ZStack {
Rectangle()
.fill(.black)
.border(.blue)
Rectangle()
.fill(.red)
.frame(width:geo.size.width/zoomFactor,
height: geo.size.height/zoomFactor)
}
}
}
}
}
struct TwoRectanglesView_Previews: PreviewProvider {
@State static var zoomFactor = 3.1
static var previews: some View {
TwoRectanglesView(zoomFactor: $zoomFactor)
}
}