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.

ScrollView

一个用于可滚动内容的 SwiftUI ScrollView 组件。

iOS
tvOS
Included in Expo Go
Recommended version:
~57.0.0

Expo UI ScrollView 与官方 SwiftUI ScrollView API 保持一致,并为其子元素提供一个可滚动容器。

安装

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.

用法

基本垂直滚动视图

一个简单的可垂直滚动的文本项列表。

ScrollViewVerticalExample.tsx
import { Host, ScrollView, VStack, Text } from '@expo/ui/swift-ui'; import { padding } from '@expo/ui/swift-ui/modifiers'; export default function ScrollViewVerticalExample() { return ( <Host style={{ flex: 1 }}> <ScrollView> <VStack spacing={8}> {Array.from({ length: 30 }, (_, i) => ( <Text key={i} modifiers={[padding({ horizontal: 16 })]}> {`Item ${i + 1}`} </Text> ))} </VStack> </ScrollView> </Host> ); }

水平滚动视图

使用 axes 属性进行水平滚动。

ScrollViewHorizontalExample.tsx
import { Host, ScrollView, HStack, RoundedRectangle } from '@expo/ui/swift-ui'; import { frame, foregroundStyle } from '@expo/ui/swift-ui/modifiers'; export default function ScrollViewHorizontalExample() { return ( <Host style={{ flex: 1 }}> <ScrollView axes="horizontal"> <HStack spacing={8}> {Array.from({ length: 20 }, (_, i) => ( <RoundedRectangle key={i} cornerRadius={12} modifiers={[ frame({ width: 100, height: 100 }), foregroundStyle(`hsl(${i * 18}, 70%, 50%)`), ]} /> ))} </HStack> </ScrollView> </Host> ); }

隐藏滚动指示器

showsIndicators 设置为 false 以隐藏滚动条。

ScrollViewHiddenIndicatorsExample.tsx
import { Host, ScrollView, VStack, Text } from '@expo/ui/swift-ui'; export default function ScrollViewHiddenIndicatorsExample() { return ( <Host style={{ flex: 1 }}> <ScrollView showsIndicators={false}> <VStack spacing={8}> {Array.from({ length: 30 }, (_, i) => ( <Text key={i}>{`Item ${i + 1}`}</Text> ))} </VStack> </ScrollView> </Host> ); }

共享滚动位置

在 JavaScript 中跟踪领先的滚动目标 id,并通过写入状态滚动到目标。使用 id 修饰器标记每个滚动目标,将内容容器包裹在 scrollTargetLayout 中,并将 scrollPosition 修饰器应用到 ScrollView。可选的 onChange 回调会在领先目标发生变化时于 JS 线程触发。

scrollPosition 修饰器也适用于其他可滚动容器,例如 LazyVStackLazyHStack

ScrollViewSharedPositionExample.tsx
import { Button, Host, ScrollView, Text, VStack, useNativeState } from '@expo/ui/swift-ui'; import { id, padding, scrollPosition, scrollTargetLayout } from '@expo/ui/swift-ui/modifiers'; import { scheduleOnUI } from 'react-native-worklets'; export default function ScrollViewSharedPositionExample() { const activeID = useNativeState<string | null>(null); return ( <Host style={{ flex: 1 }}> <VStack spacing={12}> <ScrollView modifiers={[ scrollPosition(activeID, { onChange: newID => { console.log('[JS thread] leading target:', newID); }, }), ]}> <VStack modifiers={[scrollTargetLayout()]}> {Array.from({ length: 30 }, (_, i) => ( <Text key={`item-${i}`} modifiers={[id(`item-${i}`), padding({ horizontal: 16, vertical: 12 })]}> {`Item ${i}`} </Text> ))} </VStack> </ScrollView> <Button label="从 worklet 滚动到项目 10" onPress={() => { scheduleOnUI(() => { 'worklet'; activeID.value = 'item-10'; }); }} /> </VStack> </Host> ); }

API

import { ScrollView } from '@expo/ui/swift-ui';

Component

ScrollView

Type: React.Element<ScrollViewProps>

SwiftUI ScrollView wrapper. To control scroll position, pair this with the scrollPosition(state, { onChange }) modifier and a useNativeState-backed id. Write state.value = targetId for an instant scroll, or wrap the write in withAnimation(...) from @expo/ui/swift-ui for an animated one.

ScrollViewProps

axes

Optional • Literal type: string • Default: 'vertical'

The scrollable axes. Pass 'both' to enable 2D (horizontal + vertical) scrolling.

Acceptable values are: 'vertical' | 'horizontal' | 'both'

children

Type: ReactNode

showsIndicators

Optional • Type: boolean • Default: true

Whether to show scroll indicators. For richer visibility control (e.g. 'never') or per-axis control, use the scrollIndicators(...) modifier instead.

Types

ScrollGeometry

Snapshot of a ScrollView's scroll geometry, emitted by the useScrollGeometryChange(...) and onScrollPhaseChange(...) modifiers (iOS 18+).

PropertyTypeDescription
containerHeightnumber

Height of the visible scroll container, in points.

containerWidthnumber

Width of the visible scroll container, in points.

contentHeightnumber

Total height of the scrollable content, in points.

contentOffsetXnumber

Horizontal content offset, in points.

contentOffsetYnumber

Vertical content offset, in points.

contentWidthnumber

Total width of the scrollable content, in points.

ScrollPhase

Literal type: string

Scroll phase emitted by the onScrollPhaseChange(...) modifier. Mirrors SwiftUI's ScrollPhase (iOS 18+).

Acceptable values are: 'idle' | 'tracking' | 'interacting' | 'animating' | 'decelerating'