Last active
April 7, 2020 12:10
-
-
Save pyxn/4261bea02b758623b2d9935796540e97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -------------------------------------------------------------------------------------- | |
// 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