SwiftUI.js
Components

ScrollView

ScrollView is a scrollable view that allows users to scroll through content that exceeds the visible area. ScrollView supports both vertical and horizontal scrolling.

ScrollView is a scrollable view that allows users to scroll through content that exceeds the visible area. ScrollView supports both vertical and horizontal scrolling.

Examples

Basic Usage

Preview unavailable for this example in the static docs build.

Show code
<ScrollView style={{ height: '200px' }}>
      <VStack spacing={10}>
        {Array.from({ length: 20 }, (_, i) => (
          <Text key={i}>Item {i + 1}</Text>
        ))}
      </VStack>
    </ScrollView>

Vertical Scrolling

Preview unavailable for this example in the static docs build.

Show code
<ScrollView direction="vertical" style={{ height: '200px' }}>
      <VStack spacing={10}>
        {Array.from({ length: 30 }, (_, i) => (
          <Text key={i}>Vertical Item {i + 1}</Text>
        ))}
      </VStack>
    </ScrollView>

Horizontal Scrolling

Preview unavailable for this example in the static docs build.

Show code
<ScrollView direction="horizontal" style={{ width: '100%', height: '100px' }}>
      <HStack spacing={10}>
        {Array.from({ length: 20 }, (_, i) => (
          <Text key={i} style={{ whiteSpace: 'nowrap' }}>Item {i + 1}</Text>
        ))}
      </HStack>
    </ScrollView>

Scroll Indicators

Preview unavailable for this example in the static docs build.

Show code
<ScrollView showsIndicators={false} style={{ height: '200px' }}>
      <VStack spacing={10}>
        {Array.from({ length: 20 }, (_, i) => (
          <Text key={i}>Item {i + 1} (no scrollbar)</Text>
        ))}
      </VStack>
    </ScrollView>

With Spacer

Preview unavailable for this example in the static docs build.

Show code
<ScrollView style={{ height: '200px' }}>
      <VStack spacing={10}>
        <Text>Top</Text>
        <Spacer />
        {Array.from({ length: 10 }, (_, i) => (
          <Text key={i}>Item {i + 1}</Text>
        ))}
        <Spacer />
        <Text>Bottom</Text>
      </VStack>
    </ScrollView>

Long Content

Preview unavailable for this example in the static docs build.

Show code
<ScrollView style={{ height: '300px' }}>
      <VStack spacing={15}>
        {Array.from({ length: 50 }, (_, i) => (
          <Text key={i}>Long content item {i + 1}</Text>
        ))}
      </VStack>
    </ScrollView>

Custom Styling

Preview unavailable for this example in the static docs build.

Show code
<ScrollView 
      style={{ 
        height: '200px', 
        border: '1px solid #ccc',
        borderRadius: '8px',
        padding: '10px'
      }}
    >
      <VStack spacing={10}>
        {Array.from({ length: 15 }, (_, i) => (
          <Text key={i}>Styled Item {i + 1}</Text>
        ))}
      </VStack>
    </ScrollView>

Programmatic Position

Preview unavailable for this example in the static docs build.

Show code
function ProgrammaticPositionDemo() {
  const [scrollPosition, setScrollPosition] = useState<IScrollPosition>()

  return (
    <VStack spacing={12}>
      <HStack spacing={8}>
        <Button onClick={() => setScrollPosition({ target: 'top' })}>Scroll to top</Button>
        <Button onClick={() => setScrollPosition({ target: 'middle' })}>Scroll to middle</Button>
        <Button onClick={() => setScrollPosition({ target: 'bottom' })}>Scroll to bottom</Button>
      </HStack>
      <ScrollView scrollPosition={scrollPosition} style={{ height: '220px' }}>
        <VStack spacing={12}>
          <ScrollTarget scrollId="top">
            <Text>Top anchor</Text>
          </ScrollTarget>
          {Array.from({ length: 12 }, (_, i) => (
            <Text key={i}>Scrollable item {i + 1}</Text>
          ))}
          <ScrollTarget scrollId="middle">
            <Text>Middle anchor</Text>
          </ScrollTarget>
          {Array.from({ length: 12 }, (_, i) => (
            <Text key={`tail-${i}`}>Tail item {i + 1}</Text>
          ))}
          <ScrollTarget scrollId="bottom">
            <Text>Bottom anchor</Text>
          </ScrollTarget>
        </VStack>
      </ScrollView>
    </VStack>
  )
}

API

PropTypeRequiredDescription
direction'horizontal' | 'vertical'NoThe scrollable axis of the scroll view. Default: 'vertical'
showsIndicatorsbooleanNoA Boolean value that indicates whether the scroll view displays the scroll indicators. Default: true
scrollPositionIScrollPositionNoThe current scroll position of the view.
onScrollPositionChange(position: IScrollPosition) => voidNoCalled when the scroll position changes.
onScrollPhaseChange(phase: IScrollPhase) => voidNoCalled when the scroll interaction phase changes.

Inherits additional props from IBaseComponent.

Overview

ScrollView is a scrollable view that allows users to scroll through content that exceeds the visible area. ScrollView supports both vertical and horizontal scrolling.

SwiftUI Correspondence: Similar to SwiftUI's ScrollView.

Scroll Indicators

Hide scroll indicators if needed:

With Spacer

Use Spacer within ScrollView for flexible layouts:

Long Content

ScrollView handles long content efficiently:

Custom Styling

Apply custom styles to ScrollView:

Programmatic Position

Drive a scroll view from state and scroll to anchors with scrollPosition:

Notes

  • ScrollView requires explicit dimensions (height for vertical, width for horizontal)
  • Default scroll direction is vertical
  • Scroll indicators can be hidden with showsIndicators={false}
  • ScrollView supports all standard React children
  • Use ScrollView for content that may exceed container bounds
  • Use scrollPosition to keep scroll location in React state or jump to a ScrollTarget
  • Use onScrollPhaseChange to react to active scrolling versus idle states

Best Practices

  1. Dimensions: Always provide explicit dimensions for ScrollView containers
  2. Performance: For long lists, consider using LazyVStack or LazyHStack instead
  3. Nested scrolling: Avoid nested ScrollViews in the same direction
  4. Accessibility: Ensure scrollable content is accessible via keyboard navigation
  5. Mobile: Test scrolling behavior on touch devices

On this page