A Spacer is a flexible space that expands to fill available space in a layout, pushing other views apart.
Click to reveal answer
beginner
How does padding affect a view in SwiftUI?
Padding adds empty space around the content inside a view, creating space between the view's edges and its content.
Click to reveal answer
beginner
Which SwiftUI modifier adds space inside a view's border?
The .padding() modifier adds space inside the view's border, around its content.
Click to reveal answer
intermediate
What happens if you add multiple Spacers in a horizontal stack?
Multiple Spacers share the available space equally, pushing views evenly apart.
Click to reveal answer
intermediate
Can you specify padding only on certain edges in SwiftUI?
Yes, you can specify padding on specific edges using .padding(.leading), .padding(.top), etc.
Click to reveal answer
What does Spacer() do in a SwiftUI layout?
AAdds flexible empty space to push views apart
BAdds a fixed-size box
CAdds padding inside a view
DCreates a border around a view
✗ Incorrect
Spacer() creates flexible space that expands to fill available room, pushing other views apart.
How do you add padding only to the top of a view in SwiftUI?
A.padding(10)
B.padding(.top)
C.padding(.all)
D.padding(.leading)
✗ Incorrect
Use .padding(.top) to add padding only on the top edge of a view.
If you want equal space between three buttons horizontally, what should you use?
AUse .background() modifier
BAdd padding to each button
CAdd Spacer() between each button
DUse .frame(width: 100) on buttons
✗ Incorrect
Adding Spacer() between buttons pushes them apart evenly.
What is the default behavior of .padding() without parameters?
AAdds padding only on the bottom
BAdds padding only on the left
CRemoves all padding
DAdds equal padding on all sides
✗ Incorrect
Without parameters, .padding() adds equal space around all edges of the view.
Which of these is NOT true about Spacer()?
AIt adds fixed padding inside a view
BMultiple Spacers share space equally
CIt can expand to fill space
DIt helps align views in stacks
✗ Incorrect
Spacer() does not add fixed padding; it adds flexible space between views.
Explain how Spacer and padding work together to create balanced layouts in SwiftUI.
Think about how empty space and inside space affect view placement.
You got /4 concepts.
Describe how you would add space only on the left side of a button in SwiftUI.
Remember padding can target specific edges.
You got /3 concepts.
Practice
(1/5)
1. What does Spacer() do in a SwiftUI layout?
easy
A. It adds a border around the view.
B. It adds a fixed amount of space around a view.
C. It changes the color of the background.
D. It creates flexible empty space that expands to fill available space.
Solution
Step 1: Understand Spacer's role
Spacer() is used to create flexible space that grows to fill available space inside stacks.
Step 2: Compare with padding
Padding adds fixed space around views, but Spacer expands flexibly to push views apart.
Final Answer:
It creates flexible empty space that expands to fill available space. -> Option D
Quick Check:
Spacer = flexible space [OK]
Hint: Spacer expands space flexibly, padding adds fixed space [OK]
Common Mistakes:
Confusing Spacer with padding
Thinking Spacer adds fixed space
Assuming Spacer changes colors
2. Which of the following is the correct way to add 20 points of padding around a Text view in SwiftUI?
easy
A. Text("Hello").padding(20)
B. Text("Hello").padding(EdgeInsets(top: 20, leading: 20, bottom: 20, trailing: 20))
C. All of the above
D. Text("Hello").padding(.all, 20)
Solution
Step 1: Review padding syntax
SwiftUI allows padding with a simple number, with edges and length, or with EdgeInsets struct.
Step 2: Check each option
Text("Hello").padding(20) uses padding(20) which adds 20 points all around. Text("Hello").padding(.all, 20) uses padding(.all, 20) which also adds 20 points all around. Text("Hello").padding(EdgeInsets(top: 20, leading: 20, bottom: 20, trailing: 20)) uses EdgeInsets with 20 points on all sides. All are valid ways.
Final Answer:
All of the above -> Option C
Quick Check:
Multiple padding syntaxes work [OK]
Hint: All padding methods shown add 20 points correctly [OK]
Common Mistakes:
Using padding without parameters incorrectly
Confusing padding edges syntax
Missing EdgeInsets struct format
3. What will be the layout result of this SwiftUI code?
HStack {
Text("Left")
Spacer()
Text("Right")
}
medium
A. Texts appear centered with space between them.
B. "Left" text on the left, "Right" text pushed to the right edge.
C. Both texts appear close together on the left side.
D. Texts overlap in the center.
Solution
Step 1: Understand HStack and Spacer
HStack arranges views horizontally. Spacer expands to fill space between views.
Step 2: Analyze positions
Text("Left") is placed first on the left. Spacer pushes remaining space. Text("Right") is pushed to the right edge.
Final Answer:
"Left" text on the left, "Right" text pushed to the right edge. -> Option B
Quick Check:
Spacer pushes views apart horizontally [OK]
Hint: Spacer in HStack pushes views to edges [OK]
Common Mistakes:
Thinking Spacer centers views
Assuming texts overlap
Ignoring Spacer effect in HStack
4. Identify the error in this SwiftUI code snippet:
VStack {
Text("Top")
Spacer(10)
Text("Bottom")
}
medium
A. Spacer cannot take a parameter; it has no initializer with arguments.
B. Text views must be inside a ScrollView.
C. VStack requires a frame modifier to work.
D. Spacer must be replaced with padding.
Solution
Step 1: Check Spacer usage
Spacer() does not accept any parameters; it has no initializer that takes arguments.
Step 2: Validate other options
Text views can be inside VStack without ScrollView. VStack works without frame. Spacer and padding serve different purposes.
Final Answer:
Spacer cannot take a parameter; it has no initializer with arguments. -> Option A
Quick Check:
Spacer() has no parameters [OK]
Hint: Spacer() never takes parameters [OK]
Common Mistakes:
Trying to set Spacer size via parameters
Confusing Spacer with padding
Thinking VStack needs ScrollView always
5. You want to create a vertical layout with three Text views spaced evenly in the screen height. Which SwiftUI code achieves this using Spacer and padding?