I hope this smaller technique project proved a welcome break after our long app projects, but I hope even more that you’re really starting to have a good mental model of how SwiftUI’s layout system works. That three step layout system might sound simple, but it takes time to fully understand the ramifications it has.
As for GeometryReader
, it’s one of those things you can get by perfectly fine without even thinking about, and that’s fine. But sometimes – just sometimes – you need a little extra power that you can't get from container relative frames or other SwiftUI options, and that's where GeometryReader
can come to the rescue.
Anyone can sit through a tutorial, but it takes actual work to remember what was taught. It’s my job to make sure you take as much from these tutorials as possible, so I’ve prepared a short review to help you check your learning.
Click here to review what you learned in this project.
One of the best ways to learn is to write your own code as often as possible, so here are three challenges for you to complete to experiment with your knowledge of GeometryReader
.
First, put your ContentView
back to the spinning color rows example we had:
struct ContentView: View {
let colors: [Color] = [.red, .green, .blue, .orange, .pink, .purple, .yellow]
var body: some View {
GeometryReader { fullView in
ScrollView(.vertical) {
ForEach(0..<50) { index in
GeometryReader { proxy in
Text("Row #\(index)")
.font(.title)
.frame(maxWidth: .infinity)
.background(colors[index % 7])
.rotation3DEffect(.degrees(proxy.frame(in: .global).minY - fullView.size.height / 2) / 5, axis: (x: 0, y: 1, z: 0))
}
.frame(height: 40)
}
}
}
}
}
With that done:
Color(hue:saturation:brightness:)
initializer, feeding in varying values for the hue.Each of those will require a little trial and error from you to find values that work well. Regardless, you should use max()
to handle the scaling so that views don’t go smaller than half their size, and use min()
with the hue so that hue values don’t go beyond 1.0.
Hacking with Swift+ subscribers can get a complete video solution for this checkpoint here: Solution to Layout and Geometry. If you don’t already subscribe, you can start a free trial today.
GO FURTHER, FASTER Unleash your full potential as a Swift developer with the all-new Swift Career Accelerator: the most comprehensive, career-transforming learning resource ever created for iOS development. Whether you’re just starting out, looking to land your first job, or aiming to become a lead developer, this program offers everything you need to level up – from mastering Swift’s latest features to conquering interview questions and building robust portfolios.
Link copied to your pasteboard.