简体   繁体   English

React Native KeyboardAvoidingView 覆盖最后的文本输入

[英]React Native KeyboardAvoidingView covers last text input

I'm using React Native's Keyboard Avoiding View with the behavior set to padding (testing on Android).我正在使用 React Native 的键盘避免视图,并将行为设置为填充(在 Android 上测试)。

I have multiple TextInputs on my screen.我的屏幕上有多个 TextInputs。 When I click the final TextInput, the keyboard covers it.当我单击最后一个 TextInput 时,键盘会覆盖它。 I am now able to scroll down due to padding added from KeyboardAvoidingView, but it would be ideal to have it auto scroll on focus.由于从 KeyboardAvoidingView 添加了填充,我现在可以向下滚动,但最好让它自动滚动到焦点上。

<Content>
  <KeyboardAvoidingView behavior='padding'>
    <TextInput placeholder='Example 1' />
    <TextInput placeholder='Example 2' />
    <TextInput placeholder='Example 3' />
    <TextInput placeholder='Example 4' />
    <TextInput placeholder='Example 5' />
    <TextInput placeholder='Example 6' />
    <TextInput placeholder='Example 7' />
  </KeyboardAvoidingView>
</Content>

there is prop called keyboardVerticalOffset that you can pass to the KeyboardAvoidingView that will change how much the keyboard moves past the textInput.有一个名为 keyboardVerticalOffset 的道具,您可以将它传递给 KeyboardAvoidingView,它会改变键盘移过 textInput 的距离。 Sample of my code:我的代码示例:

const keyboardVerticalOffset = Platform.OS === 'ios' ? 40 : 0

    return (
      <KeyboardAvoidingView behavior='position' keyboardVerticalOffset={keyboardVerticalOffset}>
        <ListView .../>
      <KeyboardAvoidingView/>
    )

Depending on platform, Android or IOS, implementation can be vary a little.根据平台、Android 或 IOS,实现可能略有不同。 This is how I did.我就是这样做的。

Add android:windowSoftInputMode="adjustResize" at AndroidManifest.xml,在 AndroidManifest.xml 中添加android:windowSoftInputMode="adjustResize"

 <activity
    android:name=".MainActivity"
    android:launchMode="singleTask"
    android:label="@string/app_name"
    android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
    android:windowSoftInputMode="adjustResize">

 </activity>

In your container在您的容器中

    <KeyboardAvoidingView
      behavior={Platform.OS === "ios" ? "padding" : null}
      keyboardVerticalOffset={Platform.OS === "ios" ? 64 : 0}>
      <ScrollView>
        {...}
      </ScrollView>
    </KeyboardAvoidingView>

keyboardVerticalOffset tells how much the keyboard moves past the textInput. keyboardVerticalOffset告诉键盘移过 textInput 的距离。

react-native-keyboard-aware-scroll-view react-native-keyboard-aware-scroll-view

I found it super simple to use and it worked great in both Android and iOS.我发现它使用起来非常简单,而且在 Android 和 iOS 上都运行良好。

It supports older versions of RN too.它也支持旧版本的 RN。

Initially I tried the KeyboardAvoidingView but on IOS not even最初我尝试了KeyboardAvoidingView但在 IOS 上甚至没有

behavior='position' with keyboardVerticalOffset worked properly.带有keyboardVerticalOffset behavior='position'工作正常。

They used to overlap some content in a strange way.他们过去常常以一种奇怪的方式重叠一些内容。

I have:我有:

RN 0.53.3
react-native-keyboard-aware-scroll-view 0.6.0

I added a few more details about my use case here:我在此处添加了有关我的用例的更多详细信息:

https://stackoverflow.com/a/51151496/1979861 https://stackoverflow.com/a/51151496/1979861

To add to @Maxwell's answer, sometimes you may need to scroll further than the end of the scroll view to get a component into view, since the added padding is not the full height of the keyboard.要添加到@Maxwell 的答案中,有时您可能需要滚动到滚动视图的末尾才能看到组件,因为添加的填充不是键盘的整个高度。 Full example below using scrollTo() with y offset as the height of the text input.下面的完整示例使用 scrollTo(),y 偏移量作为文本输入的高度。

import React, { Component } from 'react'
import {
    KeyboardAvoidingView,
    ScrollView,
    View,
    TextInput
} from 'react-native'


export default class Test extends Component {
    render() {
        return (
            <ScrollView style = {{flex:1, backgroundColor: 'white'}} ref = 'scroll'>
              <KeyboardAvoidingView behavior='position' style = {{backgroundColor: 'white', flex: 1}}>
                    <View style = {{height: 400}}/>
                    <TextInput style = {{height: 60}} placeholder='Example 1' />
                    <TextInput style = {{height: 60}} placeholder='Example 2' />
                    <TextInput style = {{height: 60}} placeholder='Example 3' />
                    <TextInput style = {{height: 60}} placeholder='Example 4' onFocus = {() => this.refs['scroll'].scrollTo({y: 60})}/>
              </KeyboardAvoidingView>
            </ScrollView>
        )
    } 
}

based on @Richard Millen something change in this styles基于@Richard Millen,这种风格有所改变

<ScrollView
  contentContainerStyle={{
    flexGrow: 1,
    padding: 20
  }}
>
  <TextInput
    style = {{ minHeight: 100 }}
  />
  <TextInput
    style = {{ minHeight: 100 }}
  />
  ...
</ScrollView>

if you are using react-navigation v6 you might need如果您使用的是react-navigation v6 ,您可能需要

import { useHeaderHeight } from "@react-navigation/elements";

 const headerHeight = useHeaderHeight();

   <KeyboardAvoidingView
        behavior={Platform.OS === "ios" ? "padding" : undefined}
        style={flexGrow}
        keyboardVerticalOffset={Platform.OS === "ios" ? headerHeight + Constants.statusBarHeight : 0}
      >
   </KeyboardAvoidingView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM