@codler/react-native-keyboard-aware-scroll-view

中文文档
2.0.1发布于 2021/12/10ISCTypeScriptCommonJS近 30 天下载 2.6万

A React Native ScrollView component that resizes when the keyboard appears.

原始使用文档
文档目录12 节
  1. Supported versions
  2. Installation
  3. Usage
  4. Auto-scroll in TextInput fields
  5. Programatically scroll to any TextInput
  6. Programatically scroll to any position
  7. Register to keyboard events
  8. Android Support
  9. API
  10. Props
  11. Methods
  12. Using high order component

react-native-keyboard-aware-scroll-view

A ScrollView component that handles keyboard appearance and automatically scrolls to focused TextInput.

Scroll demo

Supported versions

  • v2.0.0 requires RN>=0.65.1
  • v1.0.0 requires RN>=0.63.0

Installation

Installation can be done through npm:

npm i @codler/react-native-keyboard-aware-scroll-view --save

Usage

You can use the KeyboardAwareScrollView, KeyboardAwareSectionList or the KeyboardAwareFlatList components. They accept ScrollView, SectionList and FlatList default props respectively and implement a custom high order component called KeyboardAwareHOC to handle keyboard appearance. The high order component is also available if you want to use it in any other component.

Import react-native-keyboard-aware-scroll-view and wrap your content inside it:

import { KeyboardAwareScrollView } from '@codler/react-native-keyboard-aware-scroll-view'
<KeyboardAwareScrollView>
  <View>
    <TextInput />
  </View>
</KeyboardAwareScrollView>

Auto-scroll in TextInput fields

As of v0.1.0, the component auto scrolls to the focused TextInput 😎. For versions v0.0.7 and older you can do the following.

Programatically scroll to any TextInput

In order to scroll to any TextInput field, you can use the built-in method scrollToFocusedInput. Example:

_scrollToInput (reactNode: any) {
  // Add a 'scroll' ref to your ScrollView
  this.scroll.props.scrollToFocusedInput(reactNode)
}
<KeyboardAwareScrollView
  innerRef={ref => {
    this.scroll = ref
  }}>
  <View>
    <TextInput
      onFocus={(event: Event) => {
        // `bind` the function if you're using ES6 classes
        this._scrollToInput(ReactNative.findNodeHandle(event.target))
      }}
    />
  </View>
</KeyboardAwareScrollView>

Programatically scroll to any position

There's another built-in function that lets you programatically scroll to any position of the scroll view:

this.scroll.props.scrollToPosition(0, 0)

Register to keyboard events

You can register to ScrollViewResponder events onKeyboardWillShow and onKeyboardWillHide:

<KeyboardAwareScrollView
  onKeyboardWillShow={(frames: Object) => {
    console.log('Keyboard event', frames)
  }}>
  <View>
    <TextInput />
  </View>
</KeyboardAwareScrollView>

Android Support

First, Android natively has this feature, you can easily enable it by setting windowSoftInputMode in AndroidManifest.xml. Check here.

But if you want to use feature like extraHeight, you need to enable Android Support with the following steps:

  • Make sure you are using react-native 0.46 or above.
  • Set windowSoftInputMode to adjustPan in AndroidManifest.xml.
  • Set enableOnAndroid property to true.

Android Support is not perfect, here is the supported list:

PropAndroid Support
viewIsInsideTabBarYes
resetScrollToCoordsYes
enableAutomaticScrollYes
extraHeightYes
extraScrollHeightYes
enableResetScrollToCoordsYes
keyboardOpeningTimeNo

API

Props

All the ScrollView/FlatList props will be passed.

PropTypeDescription
innerRefFunctionCatch the reference of the component.
viewIsInsideTabBarbooleanAdds an extra offset that represents the TabBarIOS height.
resetScrollToCoordsObject: {x: number, y: number}Coordinates that will be used to reset the scroll when the keyboard hides.
enableAutomaticScrollbooleanWhen focus in TextInput will scroll the position, default is enabled.
extraHeightnumberAdds an extra offset when focusing the TextInputs.
extraScrollHeightnumberAdds an extra offset to the keyboard. Useful if you want to stick elements above the keyboard.
enableResetScrollToCoordsbooleanLets the user enable or disable automatic resetScrollToCoords.
keyboardOpeningTimenumberSets the delay time before scrolling to new position, default is 250
enableOnAndroidbooleanEnable Android Support

Methods

Use innerRef to get the component reference and use this.scrollRef.props to access these methods.

MethodParameterDescription
getScrollRespondervoidGet ScrollResponder
scrollToPositionx: number, y: number, animated: bool = trueScroll to specific position with or without animation.
scrollToEndanimated?: bool = trueScroll to end with or without animation.
scrollIntoViewelement: React.Element<*>, options: { getScrollPosition: ?(parentLayout, childLayout, contentOffset) => { x: number, y: number, animated: boolean } }Scrolls an element inside a KeyboardAwareScrollView into view.

Using high order component

Enabling any component to be keyboard-aware is very easy. Take a look at the code of KeyboardAwareFlatList:

/* @flow */

import { FlatList } from 'react-native'
import listenToKeyboardEvents from './KeyboardAwareHOC'

export default listenToKeyboardEvents(FlatList)

The HOC can also be configured. Sometimes it's more convenient to provide a static config than configuring the behavior with props. This HOC config can be overriden with props.

/* @flow */

import { FlatList } from 'react-native'
import listenToKeyboardEvents from './KeyboardAwareHOC'

const config = {
  enableOnAndroid: true,
  enableAutomaticScroll: true
}

export default listenToKeyboardEvents(config)(FlatList)

The available config options are:

{
  enableOnAndroid: boolean,
  contentContainerStyle: ?Object,
  enableAutomaticScroll: boolean,
  extraHeight: number,
  extraScrollHeight: number,
  enableResetScrollToCoords: boolean,
  keyboardOpeningTime: number,
  viewIsInsideTabBar: boolean,
  refPropName: string,
  extractNativeRef: Function
}

版本与发布记录

@codler/react-native-keyboard-aware-scroll-view 共发布 4 个版本,首次发布于 2020/07/12;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.1latest2021/12/104 年前
2.0.02021/08/245 年前
1.0.12020/07/126 年前
1.0.02020/07/126 年前

关于 @codler/react-native-keyboard-aware-scroll-view 的常见问题

@codler/react-native-keyboard-aware-scroll-view 是什么?

A React Native ScrollView component that resizes when the keyboard appears.

如何安装 @codler/react-native-keyboard-aware-scroll-view?

在项目目录运行 npm install @codler/react-native-keyboard-aware-scroll-view,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react-native(>=0.65.1)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@codler/react-native-keyboard-aware-scroll-view 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@codler/react-native-keyboard-aware-scroll-view 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@codler/react-native-keyboard-aware-scroll-view 可以商用吗?

@codler/react-native-keyboard-aware-scroll-view 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@codler/react-native-keyboard-aware-scroll-view 的下载量有多少?

根据 npm 官方下载统计,@codler/react-native-keyboard-aware-scroll-view 在 2026/10/10 – 2026/11/08 这 30 天内被下载 25,506 次,日均约 850 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@codler/react-native-keyboard-aware-scroll-view 还在维护吗?

最新版本 2.0.1 发布于 2021/12/10(4 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2020/07/12。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。