Search code examples
swiftswiftui

Why it is throw an error as "'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead" in SwiftUI?


I have project in SwiftUI 2.0 but when I update to SwiftUI 3.0 it is throw an error for

windows

as a

windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead

any idea?

.padding(.top, UIApplication.shared.windows.first?.safeAreaInsets.top)

Solution

  • Well, the warning message reflects the essence of the problem pretty fully.

    Apple really deprecated UIApplication.shared.windows, so to fix your warning, instead of UIApplication.shared.windows.first? you should use:

    UIApplication
    .shared
    .connectedScenes
    .flatMap { ($0 as? UIWindowScene)?.windows ?? [] }
    .first { $0.isKeyWindow }
    

    Then, your .padding view modifier will look like this:

    .padding(.top, UIApplication
                        .shared
                        .connectedScenes
                        .flatMap { ($0 as? UIWindowScene)?.windows ?? [] }
                        .first { $0.isKeyWindow }?.safeAreaInsets.top)