Issue #842
Declare EnvironmentKey
and read safeAreaInsets
from key window in connectedScenes
struct SafeAreaInsetsKey: EnvironmentKey {
static var defaultValue: EdgeInsets {
UIApplication.shared.keyWindow?.safeAreaInsets.swiftUIInsets ?? EdgeInsets()
}
}
private extension UIEdgeInsets {
var swiftUIInsets: EdgeInsets {
EdgeInsets(top: top, leading: left, bottom: bottom, trailing: right)
}
}
private extension UIApplication {
var keyWindow: UIWindow? {
connectedScenes
.compactMap {
$0 as? UIWindowScene
}
.flatMap {
$0.windows
}
.first {
$0.isKeyWindow
}
}
}
struct MessageBar: View {
@Environment(\.safeAreaInsets) private var safeAreaInsets
}