0
0
iOS Swiftmobile~20 mins

Alignment and spacing in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Alignment and Spacing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
Understanding SwiftUI VStack Alignment
What will be the horizontal alignment of the text views inside this VStack by default?
iOS Swift
VStack {
  Text("Hello")
  Text("World")
}
ATrailing aligned horizontally
BLeading aligned horizontally
CJustified alignment horizontally
DCenter aligned horizontally
Attempts:
2 left
💡 Hint
Think about the default alignment behavior of VStack in SwiftUI.
ui_behavior
intermediate
2:00remaining
Spacing in HStack
Given this HStack, what is the space between the two Text views?
iOS Swift
HStack(spacing: 20) {
  Text("Left")
  Text("Right")
}
A20 points of space between the texts
BDefault system spacing between the texts
CNo space between the texts
D40 points of space between the texts
Attempts:
2 left
💡 Hint
Look at the spacing parameter in the HStack initializer.
lifecycle
advanced
2:00remaining
Effect of Padding on View Size
What happens to the size of a SwiftUI Text view when you add .padding() modifier?
iOS Swift
Text("Hello")
  .padding()
AThe view becomes invisible
BThe view size increases by adding space around the text
CThe view size remains the same
DThe view size decreases to fit the text tightly
Attempts:
2 left
💡 Hint
Padding adds empty space around the content inside the view.
📝 Syntax
advanced
2:00remaining
Correct Syntax for Center Alignment in VStack
Which option correctly centers all child views horizontally inside a VStack?
AVStack(alignment: .trailing) { Text("A") }
BVStack(alignment: .leading) { Text("A") }
CVStack(alignment: .center) { Text("A") }
DVStack(alignment: .fill) { Text("A") }
Attempts:
2 left
💡 Hint
Check the valid alignment options for VStack.
🔧 Debug
expert
2:00remaining
Fixing Unexpected Spacing in SwiftUI Layout
You have this code but notice extra space around the Text view. What causes it?
iOS Swift
VStack {
  Text("Hello")
    .padding(.horizontal, 30)
  Text("World")
}
AThe .padding(.horizontal, 30) adds 30 points padding on left and right, increasing space
BThe VStack adds default spacing causing extra space
CText views have default margin causing extra space
DThe code has a syntax error causing layout issues
Attempts:
2 left
💡 Hint
Padding adds space inside the view's bounds around content.