【问题标题】:ScrollView can not scroll to the bottom when keyboard is open in react-native.[IOS]在 react-native 中打开键盘时,ScrollView 无法滚动到底部。[IOS]
【发布时间】:2021-05-05 07:36:13
【问题描述】:

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

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

我的工作环境:-

expo 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;

【问题讨论】:

    标签: ios react-native expo scrollview


    【解决方案1】:

    您可以像这样使用 KeyboardAwareScrollView:

    <KeyboardAwareScrollView  keyboardShouldPersistTaps={'always'}
            style={{flex:1}}
            showsVerticalScrollIndicator={false}>
        {/* Your code goes here*/}
    </KeyboardAwareScrollView>
    

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

    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
    });
    

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

    这里有更多关于样式表的信息: https://reactnative.dev/docs/stylesheet

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-21
      • 1970-01-01
      • 2016-11-11
      • 1970-01-01
      • 2020-03-09
      • 2019-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多