- Use ZStack to layer multiple views dynamically.
- Apply overlays and backgrounds for stylish effects.
- Create complex UI components like badges, watermarks, and image overlays.
Let’s dive in! 🚀
What is ZStack?
- The Color.blue view fills the entire frame.
- The Text view is automatically centered on top.
- The .cornerRadius(15) adds rounded corners for a sleek look.
Positioning Views Inside ZStack
- .alignment: .bottomTrailing moves content to the bottom-right.
- .padding() ensures spacing from the edges.
Perfect for:
- Floating buttons.
- Notifications or badges.
- Custom tooltips or hints.
Overlays: Adding Layers to Views
- .overlay(Text(…), alignment: .topTrailing) places the text on top of the image.
- The .background(Color.black) ensures readability.
Perfect for:
- Badges on profile images.
- Watermarks over content.
- Status indicators on UI elements.
Backgrounds vs. Overlays
Let’s build a Profile Card with an image, name, and a verification badge.
- A RoundedRectangle as the card background.
- A profile image with a verification badge overlay.
- Shadows and padding for a clean, polished look.
Building a Watermark Effect with Overlays
- Adding copyrights to images.
- Branding elements in UI designs.
- Timestamp overlays in a media app.
- ZStack layers views on top of each other.
- .overlay() adds layers above a view, while .background() places elements behind it.
- Use .alignment or .offset() for precise positioning.
- Great for: Badges, profile cards, watermarks, and custom UI components.
By mastering ZStack and overlays, you can unlock creative UI designs with depth and layering!