iOS 14 Invalid frame dimension (negative or non-finite)

牧云@^-^@ 提交于 2020-12-05 09:58:00

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/64051332/ios-14-invalid-frame-dimension-negative-or-non-finite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!