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
2
Answers
From a ViewController:
Per Apple discussion on
UIScreen.main
deprecation:Here is an extension to find the UIScreen instance through context:
For reference, this is the same
UIScreen
instance associated with thewindowScene
in the SceneDelegate: