首页 > 解决方案 > 在某些设备上使用 UIViewControllerRepresentable 会破坏 GeometryReader

问题描述

使用 UIViewControllerRepresentable 在从纵向旋转到横向并再次返回后,会在某些设备上破坏 GeometryReader。具体来说,size.height 属性错误地报告了添加了 safeAreaInsets.top 高度的高度,而 safeAreaInsets.top 设置为 0。

受影响的设备(和模拟器)似乎包括顶部安全区域高度为 20pt 的任何设备,例如 iPhone 8 Plus 或 iPhone 6s Plus。

例如,下面的代码:

import SwiftUI

struct ContentView: View {
    var body: some View {
        GeometryReader { geo in
            VStack {
                Text("geo.size.height: \(geo.size.height), geo.safeAreaInsets.top: \(geo.safeAreaInsets.top)")
                    .padding()
                TestGeo()
            }
        }
    }
}

struct TestGeo: UIViewControllerRepresentable {
    func makeUIViewController(context: UIViewControllerRepresentableContext<Self>) -> UIViewController { return UIViewController() }
    func updateUIViewController(_ viewController: UIViewController, context: UIViewControllerRepresentableContext<Self>) {}
}

geo.size.height: 736.0, geo.safeAreaInsets.top: 0.0 在从纵向旋转到横向再旋转回来(使用上面提到的模拟器之一)后显示,它应该显示geo.size.height: 716.0, geo.safeAreaInsets.top: 20.0.

我在这里遗漏了什么,或者这是苹果的错误吗?任何解决方法?UIKit 确定屏幕高度的方法似乎也遇到了同样的错误。

标签: iosswiftuiuikit

解决方案


推荐阅读