<!--
{
  "availability" : [
    "iOS: 13.0.0 -",
    "iPadOS: 13.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.15.0 -",
    "tvOS: 13.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 6.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/ZStack",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI6ZStackV"
  },
  "title" : "ZStack"
}
-->

# ZStack

A view that overlays its subviews, aligning them in both axes.

```
@frozen struct ZStack<Content> where Content : View
```

## Overview

The `ZStack` assigns each successive subview a higher z-axis value than
the one before it, meaning later subviews appear “on top” of earlier ones.

The following example creates a `ZStack` of 100 x 100 point [`Rectangle`](/documentation/SwiftUI/Rectangle)
views filled with one of six colors, offsetting each successive subview
by 10 points so they don’t completely overlap:

```
let colors: [Color] =
    [.red, .orange, .yellow, .green, .blue, .purple]

var body: some View {
    ZStack {
        ForEach(0..<colors.count) {
            Rectangle()
                .fill(colors[$0])
                .frame(width: 100, height: 100)
                .offset(x: CGFloat($0) * 10.0,
                        y: CGFloat($0) * 10.0)
        }
    }
}
```

![Six squares of different colors, stacked atop each other, with a 10-point](images/com.apple.SwiftUI/SwiftUI-ZStack-offset-rectangles~dark@2x.png)

The `ZStack` uses an [`Alignment`](/documentation/SwiftUI/Alignment) to set the x- and y-axis coordinates of
each subview, defaulting to a [`center`](/documentation/SwiftUI/Alignment/center) alignment. In the following
example, the `ZStack` uses a [`bottomLeading`](/documentation/SwiftUI/Alignment/bottomLeading) alignment to lay
out two subviews, a red 100 x 50 point rectangle below, and a blue 50 x 100
point rectangle on top. Because of the alignment value, both rectangles
share a bottom-left corner with the `ZStack` (in locales where left is the
leading side).

```
var body: some View {
    ZStack(alignment: .bottomLeading) {
        Rectangle()
            .fill(Color.red)
            .frame(width: 100, height: 50)
        Rectangle()
            .fill(Color.blue)
            .frame(width:50, height: 100)
    }
    .border(Color.green, width: 1)
}
```

![A green 100 by 100 square containing two overlapping rectangles: on the](images/com.apple.SwiftUI/SwiftUI-ZStack-alignment~dark@2x.png)

> Note: If you need a version of this stack that conforms to the ``doc://com.apple.SwiftUI/documentation/SwiftUI/Layout``
> protocol, like when you want to create a conditional layout using
> ``doc://com.apple.SwiftUI/documentation/SwiftUI/AnyLayout``, use ``doc://com.apple.SwiftUI/documentation/SwiftUI/ZStackLayout`` instead.

## Topics

### Creating a stack

[`init(alignment:content:)`](/documentation/SwiftUI/ZStack/init(alignment:content:))

Creates an instance with the given alignment.

### Supporting symbols

[`ZStackContent3D`](/documentation/SwiftUI/ZStackContent3D)

A type that adds spacing to a [`ZStack`](/documentation/SwiftUI/ZStack).



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)
