How to get Device screen width and height in SwiftUI using GeometryReader
GeometryReader an API part of SwiftUI that allows us to obtain device width and height. Useful for determine a views constraints based on screen width and height.
How it’s done
struct myView: View {
var body: some View {
GeometryReader { geo in
Text("Device size: \(geo.size.width), geo.size.height)")
}
}
}
There it is, very simple. You can now resize views based on screen dimensions. Just use geo.size.width, or geo.size.height. I hope this was helpful. Stay tuned for more content.