This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
This is documentation for the next SDK version. For up-to-date documentation, see the latest version (SDK 57).
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