I’m targeting iOS 16 for my app in which I access the screen height and width using UIScreen.main.bounds.width
and UIScreen.main.bounds.height
so I can draw views based on these two values. I’m assigning these two values to two CGFloat
properties in the view struct as follows:
struct ContentView: View {
var width: CGFloat = UIScreen.main.bounds.width
var height: CGFloat = UIScreen.main.bounds.height
var fontSize: CGFloat
var body: some View {
// draw views here using width and height properties
}
Xcode is showing a warning message saying 'main' will be deprecated in a future version of iOS: use a UIScreen instance found through context instead: i.e, view.window.windowScene.screen
I’m not sure how to apply the answer here to my use case and I don’t want to use GeometryReader
since it just messes up the overall layout.
Any suggestions on how to obtain screen width and height in an app targeting iOS 16 and above without using GeometryReader
?
2
Answers
There is a new feature in iOS 16: Users can duplicate an application. So it looks like the user has five applications, but only one is actually running. When that happens, things like UIScreen.main don’t make sense anymore, because you are actually responsible for five apps!
Now if you have a view, that belongs to ONE of the five virtual apps, so view.window.windowScene.screen will be the UIScreen that is actually responsible for what the user sees (not the four other instances of the app in the background). In iOS 17, code like that will be deprecated. State that used to be global in iOS 15 isn’t global anymore.
Take an extreme case: Two copies of an app, both running in split screen, one on the left third, one on the right two thirds of your screen. There is nothing "global" that you could use.
Swift 5.5