4

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?

SwiftUIRookie
  • 591
  • 7
  • 16
  • Is it really important that you use the `safeAreaInsets` on the window? SwiftUI has better native support for safe areas built in than trying to query the `UIWindow` – jnpdx Nov 07 '21 at 20:23
  • 1
    Does this answer your question? [How to get rid of message " 'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead" with AdMob banner?](https://stackoverflow.com/questions/69650504/how-to-get-rid-of-message-windows-was-deprecated-in-ios-15-0-use-uiwindowsc) – lorem ipsum Nov 07 '21 at 20:40

1 Answers1

8
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/

Gal
  • 356
  • 4
  • 7
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Feb 08 '22 at 01:00