Skip to content

Instantly share code, notes, and snippets.

@jimmyhoran
Last active July 1, 2021 08:49
Show Gist options
  • Save jimmyhoran/d562df99bc45997e8b78589d3d1b8e9f to your computer and use it in GitHub Desktop.
Save jimmyhoran/d562df99bc45997e8b78589d3d1b8e9f to your computer and use it in GitHub Desktop.
Safe access to the `keyWindow` for iOS 13+ support of multiple UIScene
let keyWindow = UIApplication.shared.connectedScenes
.filter { $0.activationState == .foregroundActive }
.map { $0 as? UIWindowScene }
.compactMap { $0 }
.first?.windows
.filter { $0.isKeyWindow }
.first
// Usage e.g.
_ = keyWindow?.safeAreaInsets.bottom ?? 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment