0
0
iOS Swiftmobile~20 mins

VStack, HStack, ZStack in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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)
    }
  }
}
AThree rectangles stacked vertically: red on top, green in middle, blue at bottom
BThree rectangles stacked horizontally: red on left, green in center, blue on right
CThree rectangles overlapping exactly on top of each other, red visible
DThree rectangles arranged diagonally from top-left to bottom-right
Attempts:
2 left
💡 Hint
VStack arranges views vertically in order.
ui_behavior
intermediate
2: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")
    }
  }
}
AThe texts A, B, C are arranged randomly on the screen
BThe texts A, B, C are arranged horizontally from left to right
CThe texts A, B, C overlap each other in the same position
DThe texts A, B, C are arranged vertically from top to bottom
Attempts:
2 left
💡 Hint
HStack arranges views side by side horizontally.
ui_behavior
advanced
2: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)
    }
  }
}
AAll rectangles arranged side by side horizontally
BA yellow rectangle on top, orange in middle, purple at bottom
CA purple rectangle on top, orange in middle, yellow at bottom, all centered
DAll rectangles arranged vertically with yellow on top
Attempts:
2 left
💡 Hint
ZStack layers views on top of each other in order.
lifecycle
advanced
2: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?
AThe stack is removed and recreated from scratch every time
BThe entire stack and all its child views are always fully re-rendered
CNo views update automatically; manual refresh is needed
DOnly the views affected by the state change are re-rendered inside the stack
Attempts:
2 left
💡 Hint
SwiftUI uses a diffing system to update only changed views.
navigation
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)
    }
  }
}
AThe gray background is at bottom, with red and blue texts stacked vertically on top inside smaller frame
BThe red and blue texts overlap the gray background horizontally side by side
COnly the gray background is visible, texts are hidden behind
DThe red and blue texts appear below the gray background
Attempts:
2 left
💡 Hint
ZStack layers views; VStack arranges inside vertically.