Bird
0
0

Identify the error in this SwiftUI code that tries to add space between two buttons:

medium📝 Debug Q14 of 15
iOS Swift - SwiftUI Layout
Identify the error in this SwiftUI code that tries to add space between two buttons:
HStack {
  Button("OK") {}
  Spacer(20)
  Button("Cancel") {}
}
AButtons must be wrapped in VStack, not HStack
BButton labels must be Text views, not strings
CSpacer must be replaced with padding for spacing
DSpacer cannot take a direct integer parameter like 20
Step-by-Step Solution
Solution:
  1. Step 1: Check Spacer usage

    Spacer does not accept an integer parameter directly; it uses minLength as a named parameter.
  2. Step 2: Correct Spacer syntax

    Correct usage is Spacer(minLength: 20) or Spacer().frame(width: 20) for fixed space.
  3. Final Answer:

    Spacer cannot take a direct integer parameter like 20 -> Option D
  4. Quick Check:

    Spacer parameter must be named minLength [OK]
Quick Trick: Spacer needs named minLength parameter, not direct integer [OK]
Common Mistakes:
  • Passing integer without parameter name to Spacer
  • Confusing Spacer with padding for spacing
  • Thinking Button labels must be Text views

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes