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

NavigationStack

一个 SwiftUI NavigationStack,带有可以将视图推入其中的链接和目标。

iOS
tvOS
Included in Expo Go
Recommended version:
~58.0.15

Expo UI NavigationStack 与官方 SwiftUI NavigationStack API 相匹配,并显示一个可以在其上推送其他视图的根视图。

🌐 Expo UI NavigationStack matches the official SwiftUI NavigationStack API and shows a root view that can push other views on top of it.

轻轻点击 NavigationLink 会用相同的 value 推动 NavigationDestination。

🌐 Tapping a NavigationLink pushes the NavigationDestination with the same value.

安装

🌐 Installation

Terminal
- npx expo install @expo/ui
- yarn expo install @expo/ui
- pnpm expo install @expo/ui
- bun expo install @expo/ui

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

用法

🌐 Usage

基础导航

🌐 Basic navigation

把根视图和目的地放入堆栈中。给链接和它的目的地设置相同的 value。用 navigationTitle 修饰符设置每个屏幕的标题。

🌐 Put the root view and the destinations inside the stack. Give a link and its destination the same value. Set the title of each screen with the navigationTitle modifier.

NavigationStackExample.tsx
import { Host, List, NavigationDestination, NavigationLink, NavigationStack, Text, VStack, } from '@expo/ui/swift-ui'; import { navigationTitle, padding, } from '@expo/ui/swift-ui/modifiers'; export default function NavigationStackExample() { return ( <Host style={{ flex: 1 }}> <NavigationStack> <List modifiers={[navigationTitle('Settings')]}> <NavigationLink value="account"> <Text>Account</Text> </NavigationLink> <NavigationLink value="notifications"> <Text>Notifications</Text> </NavigationLink> </List> <NavigationDestination value="account"> <VStack modifiers={[ padding({ all: 20 }), navigationTitle('Account'), ]}> <Text>Signed in as bird@example.com</Text> </VStack> </NavigationDestination> <NavigationDestination value="notifications"> <VStack modifiers={[ padding({ all: 20 }), navigationTitle('Notifications'), ]}> <Text>All notifications are on.</Text> </VStack> </NavigationDestination> </NavigationStack> </Host> ); }

在List里,链接会显示为带有箭头的行。目标必须是堆栈的直接子元素,不管触发它的链接嵌套得有多深。

🌐 Inside a List, a link renders as a row with a chevron. A destination has to be a direct child of the stack, however deeply the link that pushes it is nested.

任何视图作为链接

🌐 Any view as a link

一个链接可以封装任何视图。

🌐 A link can wrap any view.

NavigationLinkCardExample.tsx
import { Host, HStack, Image, NavigationDestination, NavigationLink, NavigationStack, Spacer, Text, VStack, } from '@expo/ui/swift-ui'; import { font, foregroundStyle, navigationTitle, padding, } from '@expo/ui/swift-ui/modifiers'; export default function NavigationLinkCardExample() { return ( <Host style={{ flex: 1 }}> <NavigationStack> <VStack modifiers={[ padding({ all: 20 }), navigationTitle('Today'), ]}> <NavigationLink value="report"> <HStack spacing={12}> <Image systemName="chart.bar.fill" size={28} /> <VStack alignment="leading" spacing={2}> <Text modifiers={[font({ textStyle: 'headline' })]}> Weekly report </Text> <Text modifiers={[ font({ textStyle: 'caption' }), foregroundStyle('secondaryLabel'), ]}> Ready to read </Text> </VStack> <Spacer /> </HStack> </NavigationLink> </VStack> <NavigationDestination value="report"> <VStack modifiers={[ padding({ all: 20 }), navigationTitle('Weekly report'), ]}> <Text>Sessions are up by 12% on last week.</Text> </VStack> </NavigationDestination> </NavigationStack> </Host> ); }

读取并设置路径

🌐 Read and set the path

栈默认会保留自己的路径。传入 path 和 onPathChange 可以从状态驱动它。路径保存了每个被推入视图的 value,按推入顺序排列。将其设置为空数组即可返回根视图。

🌐 The stack keeps its own path by default. Pass path and onPathChange to drive it from state. The path holds the value of each pushed view, in push order. Set it to an empty array to return to the root view.

NavigationStackPathExample.tsx
import { Button, Host, List, NavigationDestination, NavigationLink, NavigationStack, Text, VStack, } from '@expo/ui/swift-ui'; import { navigationTitle, padding, } from '@expo/ui/swift-ui/modifiers'; import { useState } from 'react'; const ARTICLES = ['Nesting', 'Migration', 'Song']; export default function NavigationStackPathExample() { const [path, setPath] = useState<string[]>([]); return ( <Host style={{ flex: 1 }}> <NavigationStack path={path} onPathChange={setPath}> <List modifiers={[navigationTitle('Articles')]}> {ARTICLES.map(article => ( <NavigationLink key={article} value={article}> <Text>{article}</Text> </NavigationLink> ))} </List> {ARTICLES.map(article => ( <NavigationDestination key={article} value={article}> <VStack spacing={12} modifiers={[ padding({ all: 20 }), navigationTitle(article), ]}> <Text> The chapter about {article.toLowerCase()}. </Text> <Button label="Back to the articles" onPress={() => setPath([])} /> </VStack> </NavigationDestination> ))} </NavigationStack> </Host> ); }

在导航栏添加按钮

🌐 Add buttons to the navigation bar

把内容用 Toolbar 包起来,然后把按钮放在 Toolbar.Content 里。

🌐 Wrap the content in a Toolbar and put the buttons in Toolbar.Content.

应用接口

🌐 API

import { NavigationDestination, NavigationLink, NavigationStack, } from '@expo/ui/swift-ui';

No API data file found, sorry!

No API data file found, sorry!

No API data file found, sorry!