This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
RNHostView
一个可在 SwiftUI 中显示 React Native 视图的组件。
一个组件,用于确保 React Native 视图在 SwiftUI 组件中渲染时能够正确布局。它通过更新 shadow node 大小,将 SwiftUI 中的布局信息同步回 React Native 的 Yoga 布局系统。
当 React Native 视图放置在 BottomSheet、Popover 或 HStack 等 SwiftUI 组件中时,布局系统需要相互通信。RNHostView 可以弥合这一差距:
- 使用
matchContents时:将 shadow node 大小设置为与子 React Native 视图的固有大小一致,让 SwiftUI 父视图根据 React Native 内容调整自身大小。 - 不使用
matchContents时:将 shadow node 大小设置为与 SwiftUI 父视图的大小一致,让 React Native 内容填充可用空间(适用于flex: 1布局)。
**注意:**将单个 React Native 元素作为子元素传入。
RNHostView只测量和布局它的第一个子元素,因此请将多个视图包装在一个父级View中,并由该视图排列它们。
安装
If you are installing this in an existing React Native app, make sure to install expo in your project.
用法
使用 matchContents 的基本用法
如果你希望 SwiftUI 父视图根据 React Native 内容调整自身大小,请使用 matchContents。
不使用 matchContents 的灵活内容
在 React Native 内容中使用 flex: 1 时,请省略 matchContents 属性,以便内容填充可用的 SwiftUI 空间。
在 Popover 中使用
RNHostView 非常适合在 Popover 中显示交互式 React Native 内容。
API
import { RNHostView } from '@expo/ui/swift-ui';
Component
Type: React.Element<RNHostViewProps>
boolean • Default: falseWhen true, the RNHost will update its size in the React Native view tree to match the
children's size.
When false, the RNHost will use the size of the parent SwiftUI View.
Can be only set once on mount.
(event: LayoutChangeEvent) => void