Structure

ZStack

A view that overlays its children, aligning them in both axes. あるビュー、それは、それの子らを上に重ねます、それらを両方の軸で整列しています。

Declaration 宣言

@frozen struct ZStack<Content> where Content : View

Overview 概要

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

The following example creates a ZStack of 100 x 100 point Rectangle views filled with one of six colors, offsetting each successive child view 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

The ZStack uses an Alignment to set the x- and y-axis coordinates of each child, defaulting to a center alignment. In the following example, the ZStack uses a bottomLeading alignment to lay out two children, 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

Topics 話題

Creating a Stack スタックを作成する

Supporting Types 支援を行う型

Default Implementations 省略時実装

Relationships 関係

Conforms To 次に準拠

See Also 参照

Stacks