Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 394
Franco Avatar asked Sep 24 '20 17:09

Franco


2 Answers

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))
like image 185
stephen Avatar answered Sep 18 '22 04:09

stephen


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

Warning: Invalid frame dimension (negative or non-finite)

like image 32
Patrick Fu Avatar answered Sep 21 '22 04:09

Patrick Fu