在react-native中打开键盘时ScrollView无法滚动到底部。[IOS]

Bin*_*Jha 12 scrollview ios react-native expo

当键盘关闭时,滚动视图工作正常。但是当键盘打开时,它不会滚动到底部。不过,它在 Android 中运行良好。该问题仅存在于 iOS 上。

如果我使用react-native-keyboard-aware-scroll-view,那么问题就解决了,但我不想使用这个包。

我的工作环境:-

世博会 SDK:- 40

平台:-IOS

import React from "react";
import {
  ScrollView,
  TextInput,
  SafeAreaView,
  TouchableOpacity,
  Text,
} from "react-native";

function App() {
  return (
    <SafeAreaView style={{ flex: 1 }}>
      <ScrollView style={{ flex: 1 }}>
        <TextInput style={{ borderWidth: 2, height: 50, marginVertical: 10 }} />
        <TextInput style={{ borderWidth: 2, height: 50, marginVertical: 10 }} />
        <TextInput style={{ borderWidth: 2, height: 50, marginVertical: 10 }} />
        <TextInput style={{ borderWidth: 2, height: 50, marginVertical: 10 }} />
        <TextInput style={{ borderWidth: 2, height: 50, marginVertical: 10 }} />
        <TextInput style={{ borderWidth: 2, height: 50, marginVertical: 10 }} />
        <TextInput style={{ borderWidth: 2, height: 50, marginVertical: 10 }} />
        <TextInput style={{ borderWidth: 2, height: 50, marginVertical: 10 }} />
        <TextInput style={{ borderWidth: 2, height: 50, marginVertical: 10 }} />
        <TextInput style={{ borderWidth: 2, height: 50, marginVertical: 10 }} />
        <TextInput style={{ borderWidth: 2, height: 50, marginVertical: 10 }} />
        <TextInput style={{ borderWidth: 2, height: 50, marginVertical: 10 }} />
        <TextInput style={{ borderWidth: 2, height: 50, marginVertical: 10 }} />
        <TextInput style={{ borderWidth: 2, height: 50, marginVertical: 10 }} />
        <TouchableOpacity
          style={{ height: 50, backgroundColor: "red", marginVertical: 10 }}
        >
          <Text>Button</Text>
        </TouchableOpacity>
      </ScrollView>
    </SafeAreaView>
  );
}

export default App;
Run Code Online (Sandbox Code Playgroud)

小智 5

您可以使用 KeyboardAwareScrollView 代替,如下所示:

<KeyboardAwareScrollView  keyboardShouldPersistTaps={'always'}
        style={{flex:1}}
        showsVerticalScrollIndicator={false}>
    {/* Your code goes here*/}
</KeyboardAwareScrollView>
Run Code Online (Sandbox Code Playgroud)

还有一些额外的事情,你可以做我使用样式表,而不是每次都添加文本输入的样式,这里是一个例子:

import {StyleSheet} from 'react-native

function App() {
  return (
    <SafeAreaView style={{ flex: 1 }}>
      <TextInput style={styles.textInput} />
      <TextInput style={styles.textInput} />
      <TextInput style={styles.textInput} />
    </SafeAreaView>
  );
}


const styles = StyleSheet.create({
  textInput: {
    borderWidth: 2, 
    height: 50, 
    marginVertical: 10
});
Run Code Online (Sandbox Code Playgroud)

如果您想了解有关 KeyboardAwareScrollView 的更多信息,可以访问这里: https: //github.com/APSL/react-native-keyboard-aware-scroll-view

以及有关样式表的更多信息: https ://reactnative.dev/docs/stylesheet