KeyBoardAvoidingView隐藏内容

时间:2018-01-10 20:43:37

标签: android ios react-native tablet

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;

Login.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',

      },
});

2 个答案:

答案 0 :(得分: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>
        );
}

答案 1 :(得分:0)

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

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