How can I get the safeAreaInsets in iOS 15?

In the past, I use UIApplication.shared.windows.first?.safeAreaInsets.bottom to get the bottom safe area, but in iOS 15, there is an warning shows that windows has been deprecated. My project is written in SwiftUI, so is there a way to get the global safeAreaInsets again and fits iOS 15?

Answered by Frameworks Engineer in 681167022

The problem with your code is that you could be selecting any window, including one that is offscreen, private to the system, or otherwise incorrect for your needs, which is why UIKit deprecated the UIApplication level window list.

If you have a UIView in your UI, you can obtain the window from that view, and request its safe area insets.

Accepted Answer

The problem with your code is that you could be selecting any window, including one that is offscreen, private to the system, or otherwise incorrect for your needs, which is why UIKit deprecated the UIApplication level window list.

If you have a UIView in your UI, you can obtain the window from that view, and request its safe area insets.

I just had this problem and solved it by changing:

UIApplication.shared.windows.first(where: { $0.isKeyWindow })?.safeAreaInsets ?? .zero

to

UIApplication.shared.connectedScenes.filter({$0.activationState == .foregroundActive}).map({$0 as? UIWindowScene}).compactMap({$0}).first?.windows.filter({$0.isKeyWindow}).first?.safeAreaInsets ?? .zero

Maybe that will help somebody down the line...

You can use this

extension UIApplication {
    static var yourNameByValue: UIEdgeInsets  {
        let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene
        return scene?.windows.first?.safeAreaInsets ?? .zero
    }
}
How can I get the safeAreaInsets in iOS 15?
 
 
Q