【问题标题】:KeyBoardAvoidingView hides contentKeyBoardAvoidingView 隐藏内容
【发布时间】:2018-01-10 20:43:37
【问题描述】:

enter image description here

当前行为 KeyBoardAvoidingView 隐藏了登录按钮。 它在 Android 和 iPhone 上是一样的。 在 Android 平板电脑上它隐藏了登录按钮,但由于屏幕很大,您可以从顶部看到该按钮。

预期行为 当用户想要输入他的登录详细信息时,我希望底部的登录按钮移动到应用程序框架中。

App.js

import React from 'react';
import { StyleSheet, Text, View,TouchableOpacity, AppRegistry} from 'react-native';
import { StackNavigator } from 'react-navigation'; // 1.0.0-beta.23
import Login from './screens/Login';


class App extends React.Component {

  render() {
    return (
      <View 
          style={styles.container} >   

          <View style={styles.boxContainer}>
            <View style = {[styles.boxContainer, styles.boxOne]}>
              <Text style={styles.paragraph}>Tido</Text>
            </View>

              <View style={styles.boxContainerToggle}>
                <TouchableOpacity style={[styles.boxContainer, styles.boxTwo]} onPress={() => this.props.navigation.navigate('Login')}>
                    <Text style={styles.paragraph}>Login</Text>
                </TouchableOpacity>

                <TouchableOpacity style={[styles.boxContainer, styles.boxThree]}>
                    <Text style={styles.paragraph}>Sign Up</Text>
                </TouchableOpacity>
              </View>          
          </View> 
      </View>   
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'column',

  },
  boxContainer: {
    flex: 1, // 1:3
    alignItems: 'center',
    justifyContent: 'center',
    backgroundColor: 'white',
  },

  boxContainerToggle: {
    flex: 1, 
    flexDirection: 'row'
  },
  boxOne: {
    flex: 6, // 5:6

    justifyContent: 'space-around',
    alignItems: 'center',

  },
  boxTwo: {
    flex: 1, // 1:6
    backgroundColor: 'powderblue',
    flexDirection: 'row',
    width: '50%',
    height: '100%'

  },
  boxThree: {
    flex: 1, // 1:6
    flexDirection: 'row',
    backgroundColor: 'skyblue',
    width: '50%',
    height: '100%'
  },
  paragraph: {
    margin: 24,
    fontSize: 27,
    fontWeight: 'bold',
    textAlign: 'center',
    color: '#34495e',
  }, 
});

const appScreens = StackNavigator({
  Index: { screen: App },
  Login: { screen: Login }
})


AppRegistry.registerComponent('tido', () => appScreens);
export default appScreens;

登录.js

import React from 'react';
import {StyleSheet, View, TextInput, TouchableOpacity, Text, KeyboardAvoidingView} from 'react-native';


export default class Login extends React.Component {

    constructor(props) {
        super(props);
    }

    render() {
        return(
          <KeyboardAvoidingView behavior="padding" style={styles.container}>

                <View style={styles.boxContainer}>
                    <View style = {[styles.boxContainer, styles.boxOne]}>
                        <TextInput 
                            style={styles.inputBox} 
                            placeholder="username,email or phone"
                            placeholderTextColor="#00000030"
                            underlineColorAndroid="transparent">
                        </TextInput>

                        <TextInput 
                            style={styles.inputBox}
                            secureTextEntry={true}
                            placeholder="password"
                            placeholderTextColor="#00000030"
                            underlineColorAndroid="transparent">
                        </TextInput>
                    </View>

                     <View style={styles.boxContainerToggle}>
                         <TouchableOpacity 
                            style={[styles.boxContainer, styles.boxTwo]}>
                              <Text style={styles.paragraph}>Login</Text>
                        </TouchableOpacity>
                    </View>          
                </View>   
            </KeyboardAvoidingView>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
    },
    boxContainer: {
        flex: 1, // 1:3
        justifyContent: 'center',


      },
      boxContainerToggle: {
        flex: 1, 
        flexDirection: 'row',

      },
      boxOne: {
        flex: 5, // 5:6
        backgroundColor: 'white',
        padding: 20

      },

      boxTwo: {
        flex: 1, // 1:6
        backgroundColor: '#252525',
        flexDirection: 'row',
        width: '100%',
        height: '100%',
        alignItems: 'center'

      },
      inputBox: {
          height: 40,
          backgroundColor: '#B6B6B630',
          marginBottom: 10,
          paddingHorizontal: 10,

      },

      paragraph: {
        fontSize: 27,
        fontWeight: 'bold',
        textAlign: 'center',
        color: '#FFFFFF',

      },
});

【问题讨论】:

    标签: android ios react-native tablet


    【解决方案1】:

    问题是KeyboardAvoidingView 如果它的孩子没有设置普通大小,则无法正确应用填充。在这种特殊情况下,您需要确定屏幕尺寸并将屏幕高度(减去导航栏高度)应用于您的根 View 样式:

    import { Dimensions } from 'react-native';
    
    const { screenHeight: height } = Dimensions.get('window');
    const styles = StyleSheet.create({
        ...
        containerContent: {
            height: screenHeight - navBarHeight,
            justifyContent: 'center',
        }
        ...
    },
    

    并将其应用到您的渲染方法中:

    render() {
            return (
              <KeyboardAvoidingView behavior="padding" style={styles.container}>
                    <View style={styles.containerContent}>
                           ...
                    </View>
              </KeyboardAvoidingView>
            );
    }
    

    【讨论】:

    • 我试过了,但我仍然遇到同样的问题
    • 好的,我解决了这个问题。感谢您提供意见。
    • 你是如何得到 'navBarHeight' 的?
    • 你是怎么得到导航栏高度的?
    • @emekaokoli,如果我没记错的话,我只是在那里使用常量值。 (我相应地检查了 android/ios 导航栏的大小,并在 RN 代码中硬编码了这些措施)。然而,更深思熟虑的方法是实现一个返回该值的本机模块。
    【解决方案2】:

    我仍然看到在键盘打开时工具栏被推到可见区域之外。这是代码:

    <KeyboardAvoidingView style={styles.containerContent}>
            <View style={styles.containerContent}>
              <GiftedChat
                messages={}
                onSend={this.onSend}
                renderAvatar={null}
                user={{
                  _id: 1,
                }}
                imageStyle={{}}
              />
            </View>
          </KeyboardAvoidingView>
    const styles = StyleSheet.create({
      containerContent: {
        height: Dimensions.get('window').height - kHeaderHeight,
        justifyContent: 'center',
        flex: 1,
      },
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多