Bird
0
0

You want a square view that always fills half the screen width and keeps its height equal to its width using GeometryReader. Which code snippet correctly achieves this?

hard📝 Application Q15 of 15
iOS Swift - SwiftUI Layout
You want a square view that always fills half the screen width and keeps its height equal to its width using GeometryReader. Which code snippet correctly achieves this?
AGeometryReader { geometry in Rectangle() .frame(width: geometry.size.width / 2, height: geometry.size.width / 2) }
BGeometryReader { geometry in Rectangle() .frame(width: geometry.size.height / 2, height: geometry.size.width / 2) }
CGeometryReader { geometry in Rectangle() .frame(width: geometry.size.width / 2, height: geometry.size.height / 2) }
DGeometryReader { geometry in Rectangle() .frame(width: geometry.size.width, height: geometry.size.width / 2) }
Step-by-Step Solution
Solution:
  1. Step 1: Determine width for half screen

    Use geometry.size.width / 2 for width to fill half the screen width.
  2. Step 2: Set height equal to width

    Height must match width to keep the view square, so height = geometry.size.width / 2.
  3. Final Answer:

    Rectangle with width and height both geometry.size.width / 2 -> Option A
  4. Quick Check:

    Square size = half screen width [OK]
Quick Trick: Set width and height equal using geometry.size.width / 2 [OK]
Common Mistakes:
  • Mixing width and height from different geometry properties
  • Using height from geometry.size.height which varies by device height
  • Setting unequal width and height causing non-square shape

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes