我正在寻找一个纯粹的基于SwiftUI的解决方案,以确定iOS设备是否有一流的基础上的根视图的安全区插图.虽然这在UIKit中很容易确定,但对于SwiftUI,到目前为止我找到的唯一解决方案也是UIKittish,即:
extension UIApplication {
var currentWindow: UIWindow? {
connectedScenes
.compactMap {
$0 as? UIWindowScene
}
.flatMap {
$0.windows
}
.first {
$0.isKeyWindow
}
}
}
private struct SafeAreaInsetsKey: EnvironmentKey {
static var defaultValue: EdgeInsets {
UIApplication.shared.currentWindow?.safeAreaInsets.swiftUiInsets ?? EdgeInsets()
}
}
extension EnvironmentValues {
var safeAreaInsets: EdgeInsets {
self[SafeAreaInsetsKey.self]
}
}
private extension UIEdgeInsets {
var swiftUiInsets: EdgeInsets {
EdgeInsets(top: top, leading: left, bottom: bottom, trailing: right)
}
}
虽然SwiftUI很可能会在幕后继续使用UIKit元素,如UIWindow
,因此上面的解决方案将在future 几年继续工作,但我仍然想知道是否有一个纯粹的基于SwiftUI的解决方案来解决这个问题.