Declaration
import { ZStack } from 'swiftui-react-native';@frozen nonisolated struct ZStack<Content> where Content : ViewInitializers
One SwiftUI initializer backs <ZStack>.
<ZStack
alignment?: SwiftUI.Alignment
>
children?: ReactNode
</ZStack>nonisolated init(
alignment: Alignment = .center,
@ContentBuilder content: () -> Content
)alignmentSwiftUI.AlignmentThe guide for aligning the subviews in this stack on both the x- and y-axes.
Default
`.center`childrenReactNodeContent. Strings become
Text; React Native views are laid out by Yoga and placed by SwiftUI.Swift
content: () -> Content
Also takes the common view props: modifiers, style and testID.
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
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)
}
}
}The ZStack uses an Alignment to set the x- and y-axis coordinates of
each subview, defaulting to a Alignment.center alignment. In the following
example, the ZStack uses a 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)
}protocol, like when you want to create a conditional layout using
AnyLayout, use ZStackLayout instead.
From Apple’s SwiftUI documentation. Code samples there are Swift.
Relationships
Conforms to
- View
See also
More in Layout.
- AnyLayoutA type-erased instance of the layout protocol.
- GeometryReaderA container view that defines its content as a function of its own size and coordinate space.
- GlassEffectContainerA view that combines multiple glass shapes into a single shape that can morph individual shapes into one another.
- GridA container view that arranges other views in a two dimensional layout.
- GridLayoutA grid that you can use in conditional layouts.
- GridRowA horizontal row in a two dimensional grid container.
- HStackA view that arranges its subviews in a horizontal line.
- HStackLayoutA horizontal container that you can use in conditional layouts.
- LazyHGridA container view that arranges its child views in a grid that grows horizontally, creating items only as needed.
- LazyHStackA view that arranges its children in a line that grows horizontally, creating items only as needed.
- LazyVGridA container view that arranges its child views in a grid that grows vertically, creating items only as needed.
- LazyVStackA view that arranges its children in a line that grows vertically, creating items only as needed.