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.

SwiftUI

使用 @expo/ui 构建原生 iOS 界面的 SwiftUI 组件。

iOS
tvOS
Recommended version:
~0.2.0-beta.9

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

安装

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.

用法

使用 @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

按钮

CircularProgress

ColorPicker

ContextMenu

DateTimePicker(日期)

DateTimePicker(时间)

仪表盘

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(分段式)

选择器(滚轮)

滑块

开关(切换)

开关(复选框)

TextField

更多

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

API

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

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