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.


title: SwiftUI sidebar_title: SwiftUI description: 使用 @expo/ui 构建原生 iOS 界面的 SwiftUI 组件。 sourceCodeUrl: 'https://github.com/expo/expo/tree/main/packages/expo-ui' packageName: '@expo/ui' platforms: ['ios', 'tvos'] isBeta: true hasVideoLink: true ---。

> 重要 此库目前处于测试阶段,可能会有破坏性更改。 它在 Expo Go 应用中不可用——请使用开发构建来试用。

@expo/ui/swift-ui 中的 SwiftUI 组件允许你使用 React Native 通过 SwiftUI 构建完全原生的 iOS 界面。

安装

Terminal
- npx expo install
- yarn expo install
- pnpm expo install
- bun expo install

If 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/swift-ui 的基础知识

Expo UI iOS Liquid Glass 教程
Expo UI iOS Liquid Glass 教程

了解如何使用新的 Expo UI 在你的 React Native 应用中构建真正的 SwiftUI 视图。

组件

BottomSheet

按钮

> 警告 无边框变体在 Apple TV 上不可用。

CircularProgress

ColorPicker

> 警告 此组件在 Apple TV 上不可用。

ContextMenu

> 注意: 也称为 DropdownMenu。

DateTimePicker(日期)

> 警告 此组件在 Apple TV 上不可用。

DateTimePicker(时间)

> 警告 此组件在 Apple TV 上不可用。

仪表盘

> 警告 此组件在 Apple TV 上不可用。

Host

一个组件,允许你将其他 @expo/ui/swift-ui 组件放入 React Native 中。它在 DOM 中的作用类似于 <svg>,在 react-native-skia 中的作用类似于 <Canvas>,其内部使用 UIHostingController 在 UIKit 中渲染 SwiftUI 视图。

由于 Host 组件是一个 React Native View,你可以向其传入 style 属性,或者传入 matchContents 属性,使 Host 组件匹配内容的大小。

包裹在 Host 中的 Button
import { Button, Host } from '@expo/ui/swift-ui'; function Example() { return ( <Host matchContents> <Button onPress={() => { console.log('Pressed'); }}> 点击 </Button> </Host> ); }
使用 flexbox 和 VStack 的 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

列表

Picker(分段式)

选择器(滚轮)

> 警告 轮盘变体在 Apple TV 上不可用。

滑块

> 警告 此组件在 Apple TV 上不可用。

开关(切换)

> 注意: 也称为 切换。

开关(复选框)

TextField

更多

Expo UI 仍在积极开发中。我们会持续添加更多功能,并且可能会更改 API。文档中的一些示例可能并非最新。如果你想查看最新更改,请查看 示例。

API

完整文档尚未提供。请使用 TypeScript 类型来探索该 API。

// 从 SwiftUI 包中导入 import { BottomSheet } from '@expo/ui/swift-ui';