【问题标题】:react-native-keyboard-aware-scroll-view not working properlyreact-native-keyboard-aware-scroll-view 无法正常工作
【发布时间】:2017-03-16 11:02:45
【问题描述】:

我正在尝试使用 react-native-keyboard-aware-scroll-view,因此键盘不会覆盖我的输入。

出于某种原因,我猜它总是认为有一个键盘处于活动状态,因为它总是压缩所有内容。

附件是正在发生的事情的图片以及代码。有没有人知道这里发生了什么?我一直在玩它一段时间,但没有运气。我正在运行 react-native v 0.33 和 react-native-keyboard-aware-scroll-view v 0.2.1。

https://www.npmjs.com/package/react-native-keyboard-aware-scroll-view

import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';


class LoginIOS extends Component{

  constructor(props) {
    super(props);
    this.login = this.login.bind(this);
    this.renderScene = this.renderScene.bind(this);
    this.state={
      username: '',
      password: ''
    };
  }

  render() {
    return (
      <Navigator
          renderScene={this.renderScene}
          navigator={this.props.navigator}
          navigationBar={
            <Navigator.NavigationBar style={{backgroundColor: 'transparent'}}
                routeMapper={NavigationBarRouteMapper} />
          } />
    );
  }

  renderScene() {
    return (
    <KeyboardAwareScrollView>
      <View style={styles.container}>
        <Spinner visible={this.state.visible} />
        <StatusBar barStyle="light-content" hidden={true}/>
        <View style={styles.topContainer}>
          <View style={styles.bannerContainer}>
            <View style={{flexDirection: 'column', flex: 1, justifyContent: 'center', alignItems: 'center'}}>
              <Image style={styles.mark} source={require('***')} />
            </View>
          </View>
          <View style={styles.credentialContainer}>
                <View style={styles.inputContainer}>
                  <Icon style={styles.inputPassword} name="person" size={28} color="#FFCD00" />
                      <View style={{flexDirection: 'row', flex: 1, marginLeft: 2, marginRight: 2, borderBottomColor: '#e0e0e0', borderBottomWidth: 2}}>
                        <TextInput
                            style={styles.input}
                            placeholder="Username"
                            autoCorrect={false}
                            autoCapitalize="none"
                            returnKeyType="next"
                            placeholderTextColor="#e0e0e0"
                            onChangeText={(text) => this.setState({username: text})}
                            value={this.state.username}

                            >

                        </TextInput>
                      </View>
                </View>

                <View style={styles.inputContainer}>
                  <Icon style={styles.inputPassword} name="lock" size={28} color="#FFCD00" />
                      <View style={{flexDirection: 'row', flex: 1, marginLeft: 2, marginRight: 2, borderBottomColor: '#e0e0e0', borderBottomWidth: 2}}>
                        <TextInput
                            style={styles.input}
                            placeholder="Password"
                            returnKeyType="done"
                            autoCorrect={false}
                            secureTextEntry={true}
                            placeholderTextColor="#e0e0e0"
                            onChangeText={(text) => this.setState({password: text})}
                            value={this.state.password}
                            onSubmitEditing={(event)=> {
                              this.login();
                            }}
                            >
                        </TextInput>
                      </View>
                </View>
                <TouchableOpacity style={styles.forgotContainer}>
                </TouchableOpacity>
            </View>

        </View>

        <TouchableHighlight
          underlayColor="#D6AB00"
          onPress={this.login}
          style={styles.signInButtonContainer}>
          <Text style={styles.signInText}>Sign In</Text>
        </TouchableHighlight>

      </View>
    </KeyboardAwareScrollView>

    );
  }

【问题讨论】:

    标签: android ios user-interface reactjs react-native


    【解决方案1】:

    个人使用 flex 解决了这个问题...

      <KeyboardAwareScrollView contentContainerStyle={{flex: 1}}>
        <View style={{flex: 1}}>
    

    【讨论】:

      【解决方案2】:

      我通过使用另一个库解决了这个问题。不知道为什么 react-native-keyboard-aware-scroll-view 不起作用,但如果你实现 react-native-keyboard-aware-view 你不应该有任何问题。

      https://www.npmjs.com/package/react-native-keyboard-aware-view

      【讨论】:

      • 看起来开发 react-native-keyboard-aware-view 的开发人员已经放弃了该项目。最后一次发布是在 3 年前。我不愿意使用它
      【解决方案3】:

      为了让它在带有 expo 的 android 中工作,我不得不添加一些东西,希望这会有所帮助

      <KeyboardAwareScrollView extraScrollHeight={100} enableOnAndroid={true} 
         keyboardShouldPersistTaps='handled'>
             <ScrollView>
            </ScrollView>
      </KeyboardAwareScrollView>
      

      【讨论】:

        【解决方案4】:

        如果有人还在为这个问题而苦苦挣扎。 对我有用的是确保enableOnAndroid = true 并在keyboardAwareScrollView 中设置marginBottom

        <KeyboardAwareScrollView style={{width: "90%",marginBottom:150}} enableOnAndroid={true}>
        

        【讨论】:

        • 如果键盘不完全是 150px,这不会失败吗?
        • @Lazerbeak12345 这不是最好的解决方案,但它适用于我正在开发的应用程序。实施 extraScrollHeight 是一个更好的选择,我相信文档中建议的一种选择。试试 extraScrollHeight={150}
        【解决方案5】:

        您也可以使用动画视图,因为滚动视图不能有绝对视图或固定组件。所以听键盘事件并进​​行调整就可以了。

        onKeyboarDidShow(e) {
          //LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut)
          Animated.timing(this.relativeBottom, {
            duration: e.duration,
            toValue: Dimensions.get('window').height-em(64)-e.endCoordinates.height
          }).start()
        }
        
        onKeyboardWillHide(e) {
          //LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut)
          Animated.timing(this.relativeBottom, {
            duration: e.duration,
            toValue: Dimensions.get('window').height-em(64)
          }).start()
        }
        
        componentWillMount() {
            this._didShowListener = Keyboard.addListener('keyboardWillShow', this.onKeyboarDidShow.bind(this));
            this._willHideListener = Keyboard.addListener('keyboardWillHide', this.onKeyboardWillHide.bind(this));
        }
        
        componentWillUnmount() {
            this._didShowListener.remove();
            this._willHideListener.remove();
        }
        

        【讨论】:

        • 什么是 em(64)?
        • 它是一个根据屏幕大小重新校准高度和宽度的功能。
        【解决方案6】:

        对我有用的设置

            bounces={false}
            showsVerticalScrollIndicator={false}
            style={{marginBottom:150}}
            enableOnAndroid={true}
            scrollEnabled={true}
            extraScrollHeight={100}
            keyboardShouldPersistTaps='handled'
            scrollToOverflowEnabled={true}
            enableAutomaticScroll={true}
        

        【讨论】:

          【解决方案7】:

          我将 react-native 版本升级到 0.59.4 并且 KeyboardAwareScrollView 停止正常工作。显然这个道具现在是强制性的:

          <KeyboardAwareScrollView
          scrollEnabled={true}
          enableAutomaticScroll={true}>
          

          【讨论】:

            【解决方案8】:

            像这样给父视图高度

            height:Dimensions.get('window').height
            

            使用flex:1 它将包装内容。

            【讨论】:

              【解决方案9】:

              只需添加 resetScrollToCoords、contentContainerStyle(样式表不需要命名为 container)和 scrollEnabled(您可以将其设置为 true,我觉得这样更有用)。它会正常工作并且很合身!

              import React from 'react';
              import { View, TextInput, Image } from 'react-native';
              import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view'
              import styles from './styles';
              import logo from './logo.png';
              
              const Demo = () => {
                return (
                  <KeyboardAwareScrollView
                    style={{  }}
                    resetScrollToCoords={{ x: 0, y: 0 }}
                    contentContainerStyle={styles.container}
                    scrollEnabled={false}
                  >
                      <Image source={logo} style={styles.logo} />
                      <TextInput
                        placeholder="Email"
                        style={styles.input}
                      />
                      <TextInput
                        placeholder="Username"
                        style={styles.input}
                      />
                      <TextInput
                        placeholder="Password"
                        style={styles.input}
                      />
                      <TextInput
                        placeholder="Confirm Password"
                        style={styles.input}
                      />
                  </KeyboardAwareScrollView>
                );
              };
              
              
              let styles = StyleSheet.create({
                  container: {
                      flex: 1,
                      flexDirection: "column"
                  },
              }),
              
              export default Demo;
              

              【讨论】:

                【解决方案10】:

                有了这个就可以了。还要确保您已添加包中提到的其他必要配置。

                _scrollToInput = (reactNode: any) => {
                this.scroll._internalFiberInstanceHandleDEV.memoizedProps.scrollToFocusedInput(
                  reactNode,
                );
                

                };

                【讨论】:

                  猜你喜欢
                  • 2019-04-02
                  • 2020-01-20
                  • 1970-01-01
                  • 2020-09-17
                  • 1970-01-01
                  • 1970-01-01
                  • 2018-06-13
                  • 2021-10-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多