ios - 在某些设备上使用 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 确定屏幕高度的方法似乎也遇到了同样的错误。
解决方案
推荐阅读
- ios - 如何在同一个VC中设置多个UIDocumentPickers
- python - 为什么我的 for 循环只检查最后一项?
- c# - 点击 ListView 中的图像,我们应该得到特定 Row 的属性值
- python - 如何替换 nan 和 null 值?
- assembly - 如何在带有 if 条件的 Assembly 中编写 mod?
- javascript - 浏览器上的 Webpack 抱怨 require 未定义
- python - 在 python django 中,是否可以从数据库表中提取数据并将其存储在数组中?
- javascript - 使用 Hostgator 的 VPS 主机上的 node.js
- domain-driven-design - 在 DDD 中建模对象连接
- c# - 初学者项目 - Pong - “Ball Script”有点问题:(