SwiftUI - 'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows

4.3k views Asked by At

I'm having the following code block:

struct StackOverflow: View {
    var body: some View {
        Text("Hello, World!")
            .padding(.bottom,UIApplication.shared.windows.first?.safeAreaInsets.bottom ?? 15)
    }
}

However, this returns the following error:

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

I tried to utilize UIWindowScene.windows but it's not working somehow. Any ideas how to translate this into the new syntax?

1

There are 1 answers

1
Gal On
struct StackOverflow: View {
    
    var body: some View {
        let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
        let window = windowScene?.windows.first

        Text("Hello, World!")
           .padding(.bottom, window?.safeAreaInsets.bottom ?? 15)
     }
}

Depends on what you need, there's more about this here:

  1. https://developer.apple.com/documentation/swiftui/text/padding(_:)-5wi61

  2. https://www.hackingwithswift.com/quick-start/swiftui/how-to-inset-the-safe-area-with-custom-content

  3. SwiftUI how to adjust different screen sizes

  4. https://developer.apple.com/news/?id=nixcb564

  5. https://developer.apple.com/design/human-interface-guidelines/ios/visual-design/adaptivity-and-layout/