2

UIViewControllerRepresentable每当在 SwiftUI 视图中使用时,我都会遇到一个错误。我已向反馈助理提交了一份报告,但需要解决它以发布我的应用程序。

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, Equatable {
    func makeUIViewController(context: UIViewControllerRepresentableContext<Self>) -> UIViewController { return UIViewController() }
    func updateUIViewController(_ viewController: UIViewController, context: UIViewControllerRepresentableContext<Self>) {}
}

请注意,GeometryReader该错误不需要出现,但它可能会阐明正在发生的事情。

重现步骤:

在 iPhone 8 Plus 模拟器等无缺口 iPhone 上以纵向运行代码。将模拟器从纵向旋转到横向。将模拟器从横向旋转到纵向。

案文如下:

geo.size.height:736.0,geo.safeAreaInsets.top:0.0

但应阅读:

geo.size.height:716.0,geo.safeAreaInsets.top:20.0

此外,文本位置移动了 20 点。

我发现手动更正布局的一个有趣方法是开始下拉通知中心,布局和安全区域高度会自行更正。

关于如何使布局本身正确的任何想法?

4

1 回答 1

0

包装在导航控制器中似乎可以解决问题,只需禁用所有不需要的 UI 效果,例如导航栏:

func makeUIViewController(context: UIViewControllerRepresentableContext<Self>) -> UIViewController {
    let navigationController = UINavigationController(rootViewController: UIViewController())
    navigationController.isNavigationBarHidden = true
    return navigationController
}
于 2022-02-24T16:50:43.690 回答