iOS 14 Invalid frame dimension (negative or non-finite)
My App uses GeometryReader with some padding to setup a View frame dimension inside a NavigationView.
Since iOS 14 i get the following error message:
Invalid frame dimension (negative or non-finite)
Here is some example code to test:
import SwiftUI
struct ContentView: View {
let padding:CGFloat = 16.0
var body: some View {
NavigationView {
GeometryReader { p in
Text("Hello, world!")
.frame(width: p.size.width - padding)
.padding()
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Removing NavigationView fix the problem, but I need the current width and height of the container View inside the NavigationView.
Any suggestion?
I think it might be a static analysis issue as .frame(width: p.size.width - padding)
could result in a negative value. Try:
.frame(width: abs(p.size.width - padding))
When I tested with macOS 11.1, I found that GeometryReader
could not return the actual size immediately at the beginning of the view init, the size obtained for the first time was incorrect, maybe need to do a dispatch async operation to avoid this warning
I ran into the same issue. I didn't get to the bottom of why being in a NavigationView
causes this, but a workaround is to wrap the whole NavigationView
in a GeometryReader
and pass the GeometryProxy
down the tree.
this helped me:
i added a Group { } around my first Element (which was a VStack with a .frame) in the body. The group has no ".frame" etc. worked on my MacOS app to get rid of this stupid issue.
or you can add something like
.frame(minWidth: 300, idealWidth: 400, maxWidth: 600, minHeight: 300, idealHeight: 400, maxHeight: 500, alignment: .top)
to your VStack (or whatever) to clearly say what dimensions you want.
edit strange this error will appear again, but not immediatly...