Bird
0
0

Which of the following is the correct syntax to horizontally center a Text view in SwiftUI?

easy📝 Syntax Q3 of 15
iOS Swift - SwiftUI Layout
Which of the following is the correct syntax to horizontally center a Text view in SwiftUI?
AText("Hello").frame(maxWidth: .infinity, alignment: .center)
BText("Hello").frame(maxWidth: .infinity, alignment: .leading)
CText("Hello").frame(width: 100, alignment: .trailing)
DText("Hello").frame(height: 50, alignment: .center)
Step-by-Step Solution
Solution:
  1. Step 1: Understand frame modifier with alignment

    frame(maxWidth: .infinity) expands width, alignment: .center centers content horizontally.
  2. Step 2: Identify correct alignment for horizontal centering

    alignment: .center centers the Text horizontally inside the frame.
  3. Final Answer:

    Text("Hello").frame(maxWidth: .infinity, alignment: .center) -> Option A
  4. Quick Check:

    Center horizontally = alignment: .center with maxWidth infinity [OK]
Quick Trick: Use maxWidth: .infinity and alignment: .center to center horizontally [OK]
Common Mistakes:
  • Using .leading or .trailing for centering
  • Setting fixed width without maxWidth
  • Aligning vertically instead of horizontally

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes