Skip to content

Instantly share code, notes, and snippets.

@pyxn
Last active April 7, 2020 12:10
Show Gist options
  • Save pyxn/4261bea02b758623b2d9935796540e97 to your computer and use it in GitHub Desktop.
Save pyxn/4261bea02b758623b2d9935796540e97 to your computer and use it in GitHub Desktop.
// --------------------------------------------------------------------------------------
// How To Use Geometry Reader to Access Device Dimensions in SwiftUI
// --------------------------------------------------------------------------------------
// Wrap the view code you wish to measure using the GeometryReader closure.
// Using "device" will provide the most clarity to measuring device view dimensions.
// Access the parent view's dimensions using "device.size.width" or "device.size.height"
// Official Docs: https://developer.apple.com/documentation/swiftui/geometryreader
import SwiftUI
struct ContentView: View {
var body: some View {
GeometryReader { device in
HStack(spacing: 0) {
Color(.red)
.edgesIgnoringSafeArea(.all)
// Accessing parent view (device) dimensions with .frame()
.frame(width: device.size.width, height: device.size.height)
Color(.blue)
.edgesIgnoringSafeArea(.all)
// Accessing parent view (device) dimensions with .frame()
.frame(width: device.size.width, height: device.size.height)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment