A View never scrolls, so a screen whose content runs past the bottom edge, such as a sign-up form with six fields, goes inside a ScrollView. Wrapping the form isn't enough on its own. A ScrollView scrolls only inside a bounded height, which the recommended setup gets from the views above it. While the keyboard is up it also keeps the first tap for itself, so a button under the fields can take two taps to respond.

Key terms

  • contentContainerStyle: the ScrollView prop whose style applies to the inner content container that wraps all of its children.
  • pagingEnabled: a ScrollView prop that makes scrolling stop on multiples of the scroll view's own size, which is how you page through views with swipes.
  • keyboardShouldPersistTaps: a ScrollView prop that sets when the keyboard stays visible after a tap, and with it whether the tapped child receives the tap.

The height comes from above

A ScrollView holds children of unbounded height inside a bounded container, and the user scrolls to reach the part that doesn't fit. So the ScrollView's own height has to be bounded. A fixed height on the ScrollView works, but the recommended way is to bound every view above it, for example by passing flex: 1 down the view stack.

The SignUpScreen example has a single wrapper between the screen and the ScrollView, <View style={styles.screen}>, and its screen: {flex: 1} style is the link that bounds the height. The title, the six fields and the terms line inside scroll vertically, the direction a ScrollView uses unless you set horizontal.

Break that link and the form can disappear. Give the SignUpScreen ScrollView style={{flex: 1}} and delete flex: 1 from screen. The wrapper is now a parent with neither flex nor a height whose only child is a flex child, the case from Sizing and Spacing Children. It ends up 0 points tall, the ScrollView inside it isn't visible, and none of the fields show.

In a deeper view stack, find the wrapper with React Native DevTools, which opens from the "Open DevTools" item in the Dev Menu. In its Components panel, select the Views above the ScrollView one at a time and read each one's style prop in the right-hand panel. A View with neither flex nor a height is the one to fix. The same panel can change props at runtime, so you can try flex: 1 there before you edit the code.

Child layout goes in contentContainerStyle

The ScrollView wraps all of its children in an inner content container, and contentContainerStyle styles that container. SignUpScreen puts its padding there, as form: {paddingVertical: 20, paddingHorizontal: 16}.

Props that position the children belong on the content container too. In a development build, ScrollView checks its style prop for alignItems and justifyContent. Put alignItems: 'center' in style and the screen fails with this error:

text
ScrollView child layout (["alignItems"]) must be applied through the contentContainerStyle prop.

Moving the same line into contentContainerStyle fixes it, because the check only reads style.

Paging sideways

horizontal arranges the children in a row, and pagingEnabled makes every scroll stop on a multiple of the ScrollView's own size. Both default to false. Turn both on, give each page the ScrollView's width, and every swipe comes to rest on a whole page.

The OnboardingPager example takes both widths from one constant, so the multiples of the scroll view's size fall on page edges:

tsx
pager: {width: PAGE_WIDTH},
page: {width: PAGE_WIDTH, padding: 24},

If you also set snapToInterval or snapToOffsets, they override pagingEnabled. On iOS, ScrollView passes pagingEnabled to the native view only when neither snap prop is set.

The first tap while the keyboard is up

Add a Create account button under the last field, type an email, and tap the button while the keyboard is still open. The keyboard closes and onPress doesn't run. The second tap works.

That is the default at work. keyboardShouldPersistTaps accepts 'never', 'handled' or 'always', and leaving it unset behaves exactly like 'never'. With 'never', while the keyboard is up, the ScrollView claims any new touch that doesn't start on a text input before the views nested inside it get a chance. It spends that tap on dismissing the keyboard, and the button never receives it.

With 'handled', the ScrollView yields to the views nested inside it. The button's Pressable handles the tap, onPress runs the first time, and the keyboard stays up. A tap on empty space, which no child handles, still closes the keyboard.

With 'always', the keyboard isn't dismissed automatically and the ScrollView doesn't catch taps, so its children can.

For a form, 'handled' gives you both behaviors. The button works on the first tap, and a tap beside the fields still closes the keyboard:

tsx
<ScrollView keyboardShouldPersistTaps="handled" contentContainerStyle={styles.form}>
  <TextInput placeholder="Email" style={styles.input} />
  <Pressable onPress={createAccount} style={styles.button}>
    <Text>Create account</Text>
  </Pressable>
</ScrollView>

keyboardDismissMode sounds similar, but it only controls whether a drag dismisses the keyboard ('none' by default, or 'on-drag'), so it doesn't change what a tap does.

Code Examples

tsx
import {ScrollView, StyleSheet, Text, TextInput, View} from 'react-native';

export default function SignUpScreen() {
  return (
    <View style={styles.screen}>
      <ScrollView contentContainerStyle={styles.form}>
        <Text style={styles.title}>Create your account</Text>
        <TextInput placeholder="Full name" style={styles.input} />
        <TextInput placeholder="Email" style={styles.input} />
        <TextInput placeholder="Phone" style={styles.input} />
        <TextInput placeholder="Street address" style={styles.input} />
        <TextInput placeholder="City" style={styles.input} />
        <TextInput placeholder="Postal code" style={styles.input} />
        <Text style={styles.terms}>Creating an account means you accept the terms of service.</Text>
      </ScrollView>
    </View>
  );
}

const styles = StyleSheet.create({
  screen: {flex: 1},
  form: {paddingVertical: 20, paddingHorizontal: 16},
  title: {fontSize: 28, fontWeight: 'bold', marginBottom: 16},
  input: {borderWidth: 1, borderColor: '#cccccc', borderRadius: 8, padding: 12, marginBottom: 12},
  terms: {color: '#666666', marginTop: 8},
});
tsx
import {ScrollView, StyleSheet, Text, View} from 'react-native';

const PAGE_WIDTH = 320;
const PAGES = ['Track your habits', 'Get a daily reminder', 'Watch your streak grow'];

export default function OnboardingPager() {
  return (
    <View style={styles.frame}>
      <ScrollView horizontal pagingEnabled style={styles.pager}>
        {PAGES.map(title => (
          <View key={title} style={styles.page}>
            <Text style={styles.pageTitle}>{title}</Text>
          </View>
        ))}
      </ScrollView>
    </View>
  );
}

const styles = StyleSheet.create({
  frame: {height: 200},
  pager: {width: PAGE_WIDTH},
  page: {width: PAGE_WIDTH, padding: 24},
  pageTitle: {fontSize: 22},
});
✓ Completed