Bird
0
0

How can you create a SwiftUI view that has a yellow background, a blue overlay with 50% opacity, and a red border around the overlay?

hard📝 Application Q9 of 15
iOS Swift - SwiftUI Layout
How can you create a SwiftUI view that has a yellow background, a blue overlay with 50% opacity, and a red border around the overlay?
AText("Label") .background(Color.yellow) .overlay(Rectangle().fill(Color.blue.opacity(0.5)).overlay(Rectangle().stroke(Color.red, lineWidth: 2)))
BText("Label") .background(Color.yellow) .overlay(Rectangle().stroke(Color.red, lineWidth: 2).background(Color.blue.opacity(0.5)))
CText("Label") .background(Color.yellow) .overlay(Rectangle().fill(Color.blue.opacity(0.5)).background(Color.red))
DText("Label") .background(Color.yellow) .overlay(Rectangle().stroke(Color.red, lineWidth: 2).opacity(0.5))
Step-by-Step Solution
Solution:
  1. Step 1: Set yellow background behind all

    Use .background(Color.yellow) to place yellow behind everything.
  2. Step 2: Create blue overlay with 50% opacity

    Use Rectangle().fill(Color.blue.opacity(0.5)) as overlay base.
  3. Step 3: Add red border around blue overlay

    Use .overlay(Rectangle().stroke(Color.red, lineWidth: 2)) on the blue rectangle to add border.
  4. Final Answer:

    Layers blue fill with red border as overlay on yellow background. -> Option A
  5. Quick Check:

    Background yellow, overlay blue with red border = Text("Label") .background(Color.yellow) .overlay(Rectangle().fill(Color.blue.opacity(0.5)).overlay(Rectangle().stroke(Color.red, lineWidth: 2))) [OK]
Quick Trick: Use nested overlays for border on colored overlays [OK]
Common Mistakes:
  • Applying border inside fill incorrectly
  • Using .background inside overlay incorrectly
  • Not layering border on top of fill

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes