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 点。
我发现手动更正布局的一个有趣方法是开始下拉通知中心,布局和安全区域高度会自行更正。
关于如何使布局本身正确的任何想法?