I always used UIScreen.main.bounds.size
to get the screen size on iOS and worked fine.
Now (in iOS 16, I believe) when I try to access the screen size with the "main" instance of UIScreen a warning is displayed:
"main' will be deprecated in a future version of iOS: Use a UIScreen instance found through context instead: i.e, view.window.windowScene.screen
So my question is: What is the correct way to get the screen size? for simplicity let's imagine we are on the iPhone with, consequently, only one screen available. Thank you
extension UIWindow {
static var current: UIWindow? {
for scene in UIApplication.shared.connectedScenes {
guard let windowScene = scene as? UIWindowScene else { continue }
for window in windowScene.windows {
if window.isKeyWindow { return window }
}
}
return nil
}
}
extension UIScreen {
static var current: UIScreen? {
UIWindow.current?.screen
}
}
This way, you can use this anywhere, just like you could with the UIScreen.main
.
UIScreen.current?.bounds.size
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With