SwiftUI
用于使用 @expo/ui 构建原生 iOS 界面的 SwiftUI 组件。
For the complete documentation index, see llms.txt. Use this Use this file to discover all available pages.
重要 此库目前处于 beta 阶段,可能会有破坏性变更。 它在 Expo Go 应用中不可用 — 请使用开发构建来试用。
@expo/ui/swift-ui 中的 SwiftUI 组件允许你使用 React Native 通过 SwiftUI 构建完全原生的 iOS 界面。
安装
- npx expo install @expo/uiIf you are installing this in an existing React Native app, make sure to install expo in your project.
用法
使用 @expo/ui/swift-ui 中的组件需要将其包装在 Host 组件中。Host 是 SwiftUI 视图的容器。
import { Host, Button } from '@expo/ui/swift-ui'; export function SaveButton() { return ( <Host style={{ flex: 1 }}> <Button variant="default">保存更改</Button> </Host> ); }
有关 Host 工作原理的深入说明,请参阅以下资源:
了解 @expo/ui/swift-ui 的基础知识

了解如何使用新的 Expo UI 在你的 React Native 应用中构建真正的 SwiftUI 视图。
组件
BottomSheet
import { BottomSheet, Host, Text } from '@expo/ui/swift-ui'; import { useWindowDimensions } from 'react-native'; const { width } = useWindowDimensions(); <Host style={{ position: 'absolute', width }}> <BottomSheet isOpened={isOpened} onIsOpenedChange={e => setIsOpened(e)}> <Text>你好,世界!</Text> </BottomSheet> </Host>
Button
警告 无边框变体在 Apple TV 上不可用。
import { Button, Host } from '@expo/ui/swift-ui'; <Host style={{ flex: 1 }}> <Button variant="default" onPress={() => { setEditingProfile(true); }}> 编辑个人资料 </Button> </Host>
CircularProgress
import { CircularProgress, Host } from '@expo/ui/swift-ui'; <Host style={{ width: 300 }}> <CircularProgress progress={0.5} color="blue" /> </Host>
ColorPicker
警告 此组件在 Apple TV 上不可用。
import { ColorPicker, Host } from '@expo/ui/swift-ui'; <Host style={{ width: 400, height: 200 }}> <ColorPicker label="选择一种颜色" selection={color} onValueChanged={setColor} /> </Host>
ContextMenu
注意: 也称为 DropdownMenu。
import { ContextMenu, Host } from '@expo/ui/swift-ui'; <Host style={{ width: 150, height: 50 }}> <ContextMenu> <ContextMenu.Items> <Button systemImage="person.crop.circle.badge.xmark" onPress={() => console.log('Pressed1')}> 你好 </Button> <Button variant="bordered" systemImage="heart" onPress={() => console.log('Pressed2')}> 喜欢它 </Button> <Picker label="Doggos" options={['very', 'veery', 'veeery', 'much']} variant="menu" selectedIndex={selectedIndex} onOptionSelected={({ nativeEvent: { index } }) => setSelectedIndex(index)} /> </ContextMenu.Items> <ContextMenu.Trigger> <Button variant="bordered"> 显示菜单 </Button> </ContextMenu.Trigger> </ContextMenu> </Host>
DateTimePicker (date)
警告 此组件在 Apple TV 上不可用。
import { DateTimePicker, Host } from '@expo/ui/swift-ui'; <Host matchContents> <DateTimePicker onDateSelected={date => { setSelectedDate(date); }} displayedComponents='date' initialDate={selectedDate.toISOString()} variant='wheel' /> </Host>
DateTimePicker (time)
警告 此组件在 Apple TV 上不可用。
import { DateTimePicker, Host } from '@expo/ui/swift-ui'; <Host matchContents> <DateTimePicker onDateSelected={date => { setSelectedDate(date); }} displayedComponents='hourAndMinute' initialDate={selectedDate.toISOString()} variant='wheel' /> </Host>
Gauge
警告 此组件在 Apple TV 上不可用。
import { Gauge, Host } from "@expo/ui/swift-ui"; <Host matchContents> <Gauge max={{ value: 1, label: '1' }} min={{ value: 0, label: '0' }} current={{ value: 0.5 }} color={[ PlatformColor('systemRed'), PlatformColor('systemOrange'), PlatformColor('systemYellow'), PlatformColor('systemGreen'), ]} type="circularCapacity" /> </Host>
Host
一个允许你在 React Native 中放置其他 @expo/ui/swift-ui 组件的组件。它的作用类似于 DOM 中的 <svg>、react-native-skia 中的 <Canvas>,其底层使用 UIHostingController 在 UIKit 中渲染 SwiftUI 视图。
由于 Host 组件是一个 React Native View,你可以向其传入 style 属性,或者传入 matchContents 属性,使 Host 组件匹配内容的大小。
import { Button, Host } from '@expo/ui/swift-ui'; function Example() { return ( <Host matchContents> <Button onPress={() => { console.log('Pressed'); }}> 点击 </Button> </Host> ); }
import { Button, Host, VStack, Text } from '@expo/ui/swift-ui'; function Example() { return ( <Host style={{ flex: 1 }}> <VStack spacing={8}> <Text>你好,世界!</Text> <Button onPress={() => { console.log('Pressed'); }}> 点击 </Button> </VStack> </Host> ); }
LinearProgress
import { LinearProgress, Host } from '@expo/ui/swift-ui'; <Host style={{ width: 300 }}> <LinearProgress progress={0.5} color="red" /> </Host>
List
import { Host, List } from '@expo/ui/swift-ui'; <Host style={{ flex: 1 }}> <List scrollEnabled={false} editModeEnabled={editModeEnabled} onSelectionChange={(items) => alert(`已选择项目的索引:${items.join(', ')}`)} moveEnabled={moveEnabled} onMoveItem={(from, to) => alert(`已将索引 ${from} 处的项目移动到索引 ${to}`)} onDeleteItem={(item) => alert(`已删除索引为 ${item} 的项目`)} listStyle='automatic' deleteEnabled={deleteEnabled} selectEnabled={selectEnabled}> {data.map((item, index) => ( <LabelPrimitive key={index} title={item.text} systemImage={item.systemImage} color={color} /> ))} </List> </Host>
Picker (segmented)
import { Host, Picker } from '@expo/ui/swift-ui'; <Host matchContents> <Picker options={['$', '$$', '$$$', '$$$$']} selectedIndex={selectedIndex} onOptionSelected={({ nativeEvent: { index } }) => { setSelectedIndex(index); }} variant="segmented" /> </Host>
Picker (wheel)
警告 轮盘变体在 Apple TV 上不可用。
import { Host, Picker } from '@expo/ui/swift-ui'; <Host style={{ height: 100 }}> <Picker options={['$', '$$', '$$$', '$$$$']} selectedIndex={selectedIndex} onOptionSelected={({ nativeEvent: { index } }) => { setSelectedIndex(index); }} variant="wheel" /> </Host>
Slider
警告 此组件在 Apple TV 上不可用。
import { Host, Slider } from '@expo/ui/swift-ui'; <Host style={{ minHeight: 60 }}> <Slider value={value} onValueChange={(value) => { setValue(value); }} /> </Host>
Switch (toggle)
注意: 也称为 Toggle。
import { Host, Switch } from '@expo/ui/swift-ui'; <Host matchContents> <Switch checked={checked} onValueChange={checked => { setChecked(checked); }} color="#ff0000" label="播放音乐" variant="switch" /> </Host>
Switch (checkbox)
import { Host, Switch } from '@expo/ui/swift-ui'; <Host matchContents> <Switch checked={checked} onValueChange={checked => { setChecked(checked); }} label="播放音乐" variant="checkbox" /> </Host>
TextField
import { Host, TextField } from '@expo/ui/swift-ui'; <Host matchContents> <TextField autocorrection={false} defaultValue="单行文本输入" onChangeText={setValue} /> </Host>
More
Expo UI 仍在积极开发中。我们会持续添加更多功能,并且可能会更改 API。文档中的一些示例可能并非最新。如果你想查看最新更改,请查看 示例。
API
完整文档尚未提供。请使用 TypeScript 类型来探索该 API。
// 从 SwiftUI 包中导入 import { BottomSheet } from '@expo/ui/swift-ui';