Reference version

This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

ZStack

一个用于重叠布局的 SwiftUI ZStack 组件。

iOS
tvOS
Included in Expo Go
Recommended version:
~57.0.0

Expo UI ZStack 与官方 SwiftUI ZStack API 保持一致,并将其子元素彼此叠加在一起。

三个彩色圆角方块沿 z 轴堆叠,并在 ZStack 中带有对角偏移

安装

Terminal
npx expo install @expo/ui

If you are installing this in an existing React Native app, make sure to install expo in your project.

用法

基本重叠堆叠

BasicZStackExample.tsx
import { Host, ZStack, Rectangle, Text } from '@expo/ui/swift-ui'; import { frame, foregroundStyle } from '@expo/ui/swift-ui/modifiers'; export default function BasicZStackExample() { return ( <Host matchContents> <ZStack> <Rectangle modifiers={[frame({ width: 100, height: 100 })]} /> <Text modifiers={[foregroundStyle('white')]}>Overlay</Text> </ZStack> </Host> ); }

使用对齐

alignment 属性控制子元素在堆叠中的位置。可用选项包括:centerleadingtrailingtopbottomtopLeadingtopTrailingbottomLeadingbottomTrailing

ZStackAlignmentExample.tsx
import { Host, ZStack, Rectangle, Circle } from '@expo/ui/swift-ui'; import { frame, foregroundStyle } from '@expo/ui/swift-ui/modifiers'; export default function ZStackAlignmentExample() { return ( <Host matchContents> <ZStack alignment="bottomTrailing"> <Rectangle modifiers={[frame({ width: 100, height: 100 }), foregroundStyle('blue')]} /> <Circle modifiers={[frame({ width: 30, height: 30 }), foregroundStyle('red')]} /> </ZStack> </Host> ); }

创建徽标叠加层

ZStackBadgeExample.tsx
import { Host, ZStack, Circle, Text, Image } from '@expo/ui/swift-ui'; import { frame, foregroundStyle } from '@expo/ui/swift-ui/modifiers'; export default function ZStackBadgeExample() { return ( <Host matchContents> <ZStack alignment="topTrailing"> <Image systemName="bell.fill" size={32} color="blue" /> <Circle modifiers={[frame({ width: 16, height: 16 }), foregroundStyle('red')]} /> </ZStack> </Host> ); }

API

import { ZStack } from '@expo/ui/swift-ui';

Component

ZStack

Type: React.Element<ZStackProps>

ZStackProps

alignment

Optional • Type: Alignment

The alignment of children within the stack.

children

Type: ReactNode