本文介绍了iOS 14无效的框架尺寸(负数或非有限数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用带有某些填充的GeometryReader在NavigationView内设置View框架尺寸.

My App uses GeometryReader with some padding to setup a View frame dimension inside a NavigationView.

自iOS 14以来,我收到以下错误消息:

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

删除NavigationView可解决此问题,但我需要NavigationView内的容器View的当前宽度和高度.

Removing NavigationView fix the problem, but I need the current width and height of the container View inside the NavigationView.

有什么建议吗?

推荐答案

我认为这可能是静态分析问题,因为.frame(width: p.size.width - padding)可能会导致负值.试试:

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

这篇关于iOS 14无效的框架尺寸(负数或非有限数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 03:08