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

工具栏

一个 SwiftUI 工具栏组件,它会将项目放置在它所封装视图的工具栏中。

iOS
tvOS
Included in Expo Go
Recommended version:
~58.0.15

Expo UI 工具栏与官方 SwiftUI toolbar(content:) API 相匹配,并在它所封装的视图中添加一个工具栏。

A Birds list with a sort button in the navigation barA Birds list with a sort button in the navigation bar

把工具栏项目放在 Toolbar.Content 中。Toolbar 里的其他内容是工具栏所属的视图。在 NavigationStack 里,项目放在导航栏上。

🌐 Put the toolbar items in Toolbar.Content. Everything else inside Toolbar is the view the toolbar belongs to. Inside a NavigationStack, the items go into the navigation bar.

安装

🌐 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

导航栏上的按钮

🌐 Buttons in the navigation bar

在 Toolbar 上使用 navigationTitle 修饰符设置标题。

🌐 Set the title with the navigationTitle modifier on Toolbar.

ToolbarExample.tsx
import { Button, Host, List, NavigationStack, Text, Toolbar, } from '@expo/ui/swift-ui'; import { navigationTitle } from '@expo/ui/swift-ui/modifiers'; import { useState } from 'react'; const BIRDS = ['Goldcrest', 'Barn Swallow', 'Grey Heron']; export default function ToolbarExample() { const [descending, setDescending] = useState(false); const sorted = [...BIRDS].sort((a, b) => descending ? b.localeCompare(a) : a.localeCompare(b) ); return ( <Host style={{ flex: 1 }}> <NavigationStack> <Toolbar modifiers={[navigationTitle('Birds')]}> <List> {sorted.map(bird => ( <Text key={bird}>{bird}</Text> ))} </List> <Toolbar.Content> <Button systemImage="arrow.up.arrow.down" label={descending ? 'Sort Z to A' : 'Sort A to Z'} onPress={() => setDescending(value => !value)} /> </Toolbar.Content> </Toolbar> </NavigationStack> </Host> ); }

工具栏项目放置

🌐 Toolbar item placement

将一个物品封装在ToolbarItem中,并设置它的placement。没有ToolbarItem的物品会使用automatic的放置方式。

🌐 Wrap an item in a ToolbarItem and set its placement. An item without a ToolbarItem uses the automatic placement.

ToolbarItemExample.tsx
import { Button, Host, List, NavigationStack, Text, Toolbar, ToolbarItem, } from '@expo/ui/swift-ui'; import { navigationTitle } from '@expo/ui/swift-ui/modifiers'; const BIRDS = ['Goldcrest', 'Barn Swallow', 'Grey Heron']; export default function ToolbarItemExample() { return ( <Host style={{ flex: 1 }}> <NavigationStack> <Toolbar modifiers={[navigationTitle('Birds')]}> <List> {BIRDS.map(bird => ( <Text key={bird}>{bird}</Text> ))} </List> <Toolbar.Content> <ToolbarItem placement="topBarTrailing"> <Button systemImage="square.and.arrow.up" label="Share" onPress={() => {}} /> </ToolbarItem> <ToolbarItem placement="bottomBar"> <Button systemImage="plus" label="Add bird" onPress={() => {}} /> </ToolbarItem> </Toolbar.Content> </Toolbar> </NavigationStack> </Host> ); }

ToolbarItem 必须在 Toolbar.Content 内。如果当前平台或操作系统版本没有提供放置位置,该项目将使用 automatic。

🌐 A ToolbarItem has to be inside Toolbar.Content. If the current platform or OS version does not offer a placement, the item uses automatic.

表格中的关闭按钮

🌐 Close button in a sheet

一个界面本身没有导航栏,所以给它的内容添加一个 NavigationStack。在 iOS 26 及更新版本上,一个带有 role="close" 但没有 label 或子视图的 Button 会显示为关闭(X)图标。在更早版本上,改用设置 label 和 systemImage="xmark"。

🌐 A sheet has no navigation bar of its own, so give its content a NavigationStack. On iOS 26 and later, a Button with role="close" and no label or children shows as a close (X) icon. On earlier versions, set a label and systemImage="xmark" instead.

ToolbarCloseButtonExample.tsx
import { BottomSheet, Button, Host, NavigationStack, Text, Toolbar, VStack, } from '@expo/ui/swift-ui'; import { navigationTitle, padding, presentationDetents, } from '@expo/ui/swift-ui/modifiers'; import { useState } from 'react'; export default function ToolbarCloseButtonExample() { const [isPresented, setIsPresented] = useState(false); return ( <Host style={{ flex: 1 }}> <BottomSheet isPresented={isPresented} onIsPresentedChange={setIsPresented} anchor={ <Button label="Show details" onPress={() => setIsPresented(true)} /> }> <NavigationStack modifiers={[presentationDetents(['medium'])]}> <Toolbar modifiers={[navigationTitle('Grey Heron')]}> <VStack alignment="leading" spacing={12} modifiers={[padding({ all: 20 })]}> <Text> Stands still at the water's edge for long spells, then strikes at fish with its dagger bill. </Text> </VStack> <Toolbar.Content> <Button role="close" onPress={() => setIsPresented(false)} /> </Toolbar.Content> </Toolbar> </NavigationStack> </BottomSheet> </Host> ); }

应用接口

🌐 API

import { Toolbar, ToolbarItem } from '@expo/ui/swift-ui';

No API data file found, sorry!