GeometryReader helps your app adjust its layout based on the available space. It makes your app look good on different screen sizes.
GeometryReader for adaptive layouts in iOS Swift
GeometryReader { geometry in
// Use geometry.size.width and geometry.size.height here
// to set sizes or positions
Text("Hello")
.frame(width: geometry.size.width / 2, height: 50)
}The geometry parameter gives you the size and position of the container.
You can use geometry.size.width and geometry.size.height to get the width and height.
GeometryReader { geometry in
Text("Half Width Button")
.frame(width: geometry.size.width / 2, height: 50)
.background(Color.blue)
.foregroundColor(.white)
}GeometryReader { geometry in
VStack {
Text("Width: \(Int(geometry.size.width))")
Text("Height: \(Int(geometry.size.height))")
}
}GeometryReader { geometry in
Circle()
.frame(width: geometry.size.width / 3, height: geometry.size.width / 3)
.position(x: geometry.size.width / 2, y: geometry.size.height / 2)
}This app shows the screen width as text and a green rectangle that fills 80% of the screen width. It adapts automatically to different screen sizes.
import SwiftUI struct ContentView: View { var body: some View { GeometryReader { geometry in VStack { Text("Screen width: \(Int(geometry.size.width))") .font(.headline) .padding() Rectangle() .fill(Color.green) .frame(width: geometry.size.width * 0.8, height: 100) .overlay(Text("80% width rectangle").foregroundColor(.white)) .cornerRadius(10) } .frame(width: geometry.size.width, height: geometry.size.height) } } } @main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } } }
GeometryReader takes all available space by default, so use frames or stacks to control layout.
Use GeometryReader inside flexible containers like VStack or ZStack for best results.
Remember to test on different devices and orientations to see how your layout adapts.
GeometryReader helps you build layouts that adapt to screen size.
You get the container size from geometry.size and use it to set widths, heights, or positions.
This makes your app look good on phones, tablets, and when rotated.
Practice
GeometryReader in SwiftUI layouts?Solution
Step 1: Understand GeometryReader role
GeometryReader provides the size and position of its container view.Step 2: Use size for adaptive layouts
This size info helps adjust child views to fit different screen sizes.Final Answer:
To get the size and position of the container to create adaptive layouts -> Option DQuick Check:
GeometryReader = adaptive layout helper [OK]
- Thinking GeometryReader fixes view size
- Confusing GeometryReader with animation tools
- Using GeometryReader for user input handling
GeometryReader closure?Solution
Step 1: Recall GeometryReader parameter
The closure parameter is usually named geometry and has a size property.Step 2: Access width correctly
The width is accessed as geometry.size.width, not geometry.width or others.Final Answer:
geometry.size.width -> Option CQuick Check:
Width = geometry.size.width [OK]
- Using geometry.width instead of geometry.size.width
- Trying geometry.frame.width which doesn't exist
- Using geometry.container.width which is invalid
GeometryReader { geometry in
Rectangle()
.fill(Color.red)
.frame(width: geometry.size.width / 2, height: 100)
}Solution
Step 1: Identify container width
The screen width is given as 300 points, so geometry.size.width = 300.Step 2: Calculate rectangle width
The frame width is geometry.size.width / 2 = 300 / 2 = 150 points.Final Answer:
150 points -> Option BQuick Check:
300 / 2 = 150 [OK]
- Using full width instead of half
- Confusing height with width
- Ignoring geometry.size and using fixed numbers
GeometryReader { geometry in
Text("Hello")
.frame(width: geometry.width, height: 50)
}Solution
Step 1: Check geometry property usage
The code uses geometry.width, but GeometryProxy has no width property directly.Step 2: Correct property to use
The correct property is geometry.size.width to get container width.Final Answer:
geometry.width is invalid; should use geometry.size.width -> Option AQuick Check:
Use geometry.size.width, not geometry.width [OK]
- Using geometry.width instead of geometry.size.width
- Thinking Text can't be inside GeometryReader
- Believing frame height must be dynamic
Solution
Step 1: Determine width for half screen
Use geometry.size.width / 2 for width to fill half the screen width.Step 2: Set height equal to width
Height must match width to keep the view square, so height = geometry.size.width / 2.Final Answer:
Rectangle with width and height both geometry.size.width / 2 -> Option AQuick Check:
Square size = half screen width [OK]
- Mixing width and height from different geometry properties
- Using height from geometry.size.height which varies by device height
- Setting unequal width and height causing non-square shape
Ready to prove it?
Pass the test and this pattern moves unknown
