Challenge - 5 Problems
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What is the layout result of this SwiftUI code?
Given the following SwiftUI code, what will be the order and position of the colored rectangles on the screen?
iOS Swift
import SwiftUI struct ContentView: View { var body: some View { VStack { Rectangle().fill(Color.red).frame(width: 100, height: 50) Rectangle().fill(Color.green).frame(width: 100, height: 50) Rectangle().fill(Color.blue).frame(width: 100, height: 50) } } }
Attempts:
2 left
💡 Hint
VStack arranges views vertically in order.
✗ Incorrect
VStack stacks views vertically in the order they are declared. So red is on top, green in the middle, and blue at the bottom.
❓ ui_behavior
intermediate2:00remaining
How does HStack arrange views?
What is the visual arrangement of views inside an HStack in SwiftUI?
iOS Swift
import SwiftUI struct ContentView: View { var body: some View { HStack { Text("A") Text("B") Text("C") } } }
Attempts:
2 left
💡 Hint
HStack arranges views side by side horizontally.
✗ Incorrect
HStack arranges its child views horizontally in the order they appear, from left to right.
❓ ui_behavior
advanced2:00remaining
What is the visual output of this ZStack code?
Consider this SwiftUI code using ZStack. Which color rectangle will be fully visible on top?
iOS Swift
import SwiftUI struct ContentView: View { var body: some View { ZStack { Rectangle().fill(Color.yellow).frame(width: 150, height: 150) Rectangle().fill(Color.orange).frame(width: 100, height: 100) Rectangle().fill(Color.purple).frame(width: 50, height: 50) } } }
Attempts:
2 left
💡 Hint
ZStack layers views on top of each other in order.
✗ Incorrect
ZStack places views stacked on top of each other. The last declared view is on top. So purple is topmost, orange below it, yellow at bottom.
❓ lifecycle
advanced2:00remaining
How does SwiftUI update views inside stacks when state changes?
If a @State variable changes inside a view containing VStack, HStack, or ZStack, what happens to the views inside these stacks?
Attempts:
2 left
💡 Hint
SwiftUI uses a diffing system to update only changed views.
✗ Incorrect
SwiftUI efficiently updates only the views affected by state changes inside stacks, not the entire stack.
expert
3:00remaining
What happens if you nest VStack inside ZStack with overlapping frames?
Given this SwiftUI code, which describes the final visual layering and position of the texts?
iOS Swift
import SwiftUI struct ContentView: View { var body: some View { ZStack { Text("Bottom").frame(width: 200, height: 200).background(Color.gray) VStack { Text("Top 1").background(Color.red) Text("Top 2").background(Color.blue) }.frame(width: 150, height: 150) } } }
Attempts:
2 left
💡 Hint
ZStack layers views; VStack arranges inside vertically.
✗ Incorrect
ZStack places the gray background at bottom. The VStack with red and blue texts is layered on top, arranged vertically inside its frame.