C. Image cannot have background or overlay modifiers
D. Multiple background modifiers cause only the first to apply
Solution
Step 1: Analyze multiple background modifiers
Applying .background() twice stacks backgrounds, but only the first background modifier applies directly behind the view, later ones get layered behind that.
Step 2: Understand layering order
The last .background(Color.red) will be behind the yellow circle background, so the red color might be hidden or layered behind unexpectedly.
Final Answer:
Multiple background modifiers cause only the first to apply -> Option D
Quick Check:
Last background behind earlier ones visually [OK]
Hint: Multiple backgrounds stack but first is closest behind view [OK]
Common Mistakes:
Thinking overlay must be last
Believing Circle can't be overlay
Assuming Image can't have modifiers
5. You want to create a button with a blue rounded rectangle background and a white checkmark icon overlay on top. Which SwiftUI code correctly achieves this layering?
hard
A. Image(systemName: "checkmark")
.background(RoundedRectangle(cornerRadius: 10).fill(Color.blue))
.overlay(Image(systemName: "checkmark").foregroundColor(.white))
B. Image(systemName: "checkmark")
.background(Color.blue.cornerRadius(10))
.overlay(Color.white.opacity(0.5))
C. Image(systemName: "checkmark")
.overlay(Color.blue.cornerRadius(10))
D. Image(systemName: "checkmark")
.background(Image(systemName: "checkmark"))
.overlay(Color.blue.cornerRadius(10))
Solution
Step 1: Add blue rounded rectangle background
Use .background(RoundedRectangle(cornerRadius: 10).fill(Color.blue)) to create a blue rounded rectangle behind the icon.
Step 2: Overlay white checkmark icon
Use .overlay(Image(systemName: "checkmark").foregroundColor(.white)) to place a white checkmark on top of the background.
Step 3: Verify layering and colors
This layering ensures the blue shape is behind and the white icon is visible on top.
Final Answer:
Image(systemName: "checkmark")
.background(RoundedRectangle(cornerRadius: 10).fill(Color.blue))
.overlay(Image(systemName: "checkmark").foregroundColor(.white)) -> Option A