SwiftUI SwiftUI Views and Layout 1 — Questions and Answers
Question 1: Which SwiftUI view is used to stack child views vertically?
- HStack
- VStack (Correct answer)
- ZStack
- Group
Correct answer: VStack
VStack arranges its child views in a vertical line.
Question 2: What modifier is used to set the background color of a SwiftUI view?
- .foregroundColor()
- .backgroundColor()
- .background() (Correct answer)
- .fill()
Correct answer: .background()
The `.background()` modifier applies a background color or view behind the modified view.
Question 3: Which view acts as a flexible spacer that expands to fill available space in a stack?
- Divider
- Spacer (Correct answer)
- EmptyView
- Rectangle
Correct answer: Spacer
Spacer expands to fill all available space along the stack's primary axis.
Question 4: What does the `.frame(width:height:)` modifier do in SwiftUI?
- Clips the view to a specific shape
- Sets the view's proposed size (Correct answer)
- Adds a border around the view
- Aligns the view in its parent
Correct answer: Sets the view's proposed size
`.frame(width:height:)` proposes a fixed size to the view and positions it within that frame.
Question 5: Which SwiftUI view overlays its children on top of each other?
- HStack
- VStack
- ZStack (Correct answer)
- List
Correct answer: ZStack
ZStack layers its child views along the Z-axis, drawing them on top of one another.
Question 6: What modifier clips a SwiftUI view to a circular shape?
- .cornerRadius()
- .clipShape(Circle()) (Correct answer)
- .mask()
- .border(Circle())
Correct answer: .clipShape(Circle())
`.clipShape(Circle())` clips the view's rendering to a circle shape.
Which SwiftUI view is used to stack child views vertically?