Skip to content

Commit 32a4046

Browse files
author
Anton Gubarenko
committed
PR info added
1 parent d1ec8dc commit 32a4046

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

swiftui-expert-skill/references/view-structure.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,16 @@ MyContainer {
202202

203203
## ZStack vs overlay/background
204204

205-
Use `ZStack` to **compose multiple peer views** that should be layered together.
205+
Use `ZStack` to **compose multiple peer views** that should be layered together and jointly define layout.
206206

207-
Prefer `overlay` / `background` when you’re **decorating a primary view without changing its measured size**.
208-
Use `ZStack` (or another container) when the “decoration” **must participate in layout sizing** (reserve space, extend visible/tappable bounds, avoid overlap with neighbors).
207+
Prefer `overlay` / `background` when you’re **decorating a primary view**.
208+
Not primarily because they don’t affect layout size, but because they **express intent and improve readability**: the view being modified remains the clear layout anchor.
209+
210+
A key difference is **size proposal behavior**:
211+
- In `overlay` / `background`, the child view implicitly adopts the size proposed to the parent when it doesn’t define its own size, making decorative attachments feel natural and predictable.
212+
- In `ZStack`, each child participates independently in layout, and no implicit size inheritance exists. This makes it better suited for peer composition, but less intuitive for simple decoration.
213+
214+
Use `ZStack` (or another container) when the “decoration” **must explicitly participate in layout sizing**—for example, when reserving space, extending tappable/visible bounds, or preventing overlap with neighboring views.
209215

210216
### Examples: Choosing Between overlay/background and ZStack
211217

0 commit comments

Comments
 (0)