Bird
0
0

What will be the layout result of this SwiftUI code?

medium📝 Predict Output Q13 of 15
iOS Swift - SwiftUI Basics
What will be the layout result of this SwiftUI code?
HStack {
  Text("Left")
  Spacer()
  Text("Right")
}
ATexts appear centered with space between them.
B"Left" text on the left, "Right" text pushed to the right edge.
CBoth texts appear close together on the left side.
DTexts overlap in the center.
Step-by-Step Solution
Solution:
  1. Step 1: Understand HStack and Spacer

    HStack arranges views horizontally. Spacer expands to fill space between views.
  2. Step 2: Analyze positions

    Text("Left") is placed first on the left.
    Spacer pushes remaining space.
    Text("Right") is pushed to the right edge.
  3. Final Answer:

    "Left" text on the left, "Right" text pushed to the right edge. -> Option B
  4. Quick Check:

    Spacer pushes views apart horizontally [OK]
Quick Trick: Spacer in HStack pushes views to edges [OK]
Common Mistakes:
  • Thinking Spacer centers views
  • Assuming texts overlap
  • Ignoring Spacer effect in HStack

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes