This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Host
一个 SwiftUI Host 组件,可在 React Native 中使用 SwiftUI 组件。
信息 如需跨平台使用,请参阅通用
Host— 它会根据平台渲染相应的原生组件。
一个可让你在 React Native 中使用其他 @expo/ui/swift-ui 组件的组件。它的作用类似于 DOM 中的 <svg>、react-native-skia 中的 <Canvas>,内部使用 UIHostingController 在 UIKit 中渲染 SwiftUI 视图。
由于 Host 组件是 React Native View,你可以向它传递 style 属性,或使用 matchContents 属性让 Host 组件适应内容尺寸。
安装
If you are installing this in an existing React Native app, make sure to install expo in your project.
用法
匹配内容尺寸
使用 matchContents 让 Host 自动调整自身大小以适应 SwiftUI 内容,而无需指定尺寸。
注意: 不要在与滚动容器(
ScrollView、List、Form、LazyHStack、LazyVStack)相同的轴上使用matchContents。matchContents会解析为 SwiftUI 的.fixedSize,使滚动容器的尺寸适应其内容。这样视口之外就没有可滚动的内容,因此滚动会悄然失效。请使用matchContents={{ vertical: true }}并配合style={{ width: '100%' }}(或滚动轴上任意有限宽度)。
使用 style 显式设置尺寸
使用 style 为 Host 设置明确的尺寸,例如使用 flex: 1 填充可用空间。
将组件放入 React Native 视图
Host 使用 SwiftUI 渲染其子组件。Host 内部的 React Native 视图(例如 View 或 ScrollView)会切换回 React Native 渲染。要在该视图中使用 SwiftUI 组件,请将组件包裹在新的 Host 中。即使视图外层的树中已经有一个 Host,也要这样做。
要将 React Native 视图放入 SwiftUI 组件中,请使用 RNHostView。
忽略键盘安全区域
当 React Native 已处理键盘避让时(例如使用 react-native-keyboard-controller),请使用 ignoreSafeArea="keyboard",以防止 SwiftUI Host 应用自身的键盘内边距。
忽略容器安全区域
使用 ignoreSafeArea="container" 仅移除容器安全区域(刘海、主屏幕指示条、状态栏和导航栏)。键盘安全区域仍然生效。
忽略所有安全区域
当你希望 SwiftUI 内容延伸到状态栏和键盘后方时,请使用 ignoreSafeArea="all";这适用于全屏叠加层或背景。
API
import { Host } from '@expo/ui/swift-ui';
Component
Type: React.Element<HostProps>
A hosting component for SwiftUI views.
ReactNodestringThe color scheme of the host view.
Acceptable values are: 'light' | 'dark'
stringControls which safe area regions the SwiftUI hosting view should ignore.
'all'- ignores all safe area insets, including the keyboard.'container'- ignores only the container safe area (notch, home indicator, status and navigation bars). The keyboard safe area still applies.'keyboard'- ignores only the keyboard safe area.
Acceptable values are: 'container' | 'all' | 'keyboard'
stringThe layout direction for the SwiftUI content. Defaults to the current locale direction from I18nManager.
Acceptable values are: 'leftToRight' | 'rightToLeft'
union • Default: falseWhen true, the host view will update its size in the React Native view tree to match the content's layout from SwiftUI. Can be only set once on mount.
Acceptable values are: boolean | {
horizontal: boolean,
vertical: boolean
}
(event: {
nativeEvent: {
height: number,
width: number
}
}) => voidCallback function that is triggered when the SwiftUI content completes its layout. Provides the current dimensions of the content, which may change as the content updates.
stringAcceptable values are: 'auto' | 'box-none' | 'none' | 'box-only'
ColorValueSeed color applied to the SwiftUI content as its tint. It propagates through the SwiftUI environment to theme interactive elements (buttons, switches, sliders, and similar controls) rendered by the children.
StyleProp<ViewStyle>boolean • Default: falseWhen true and no explicit size is provided, the host will use the viewport size as the proposed size for SwiftUI layout.
This is particularly useful for SwiftUI views that need to fill their available space, such as Form.