TIL: RN | 키보드 영역 (keyboardAvoidingView) - 221006

Lumpen·2022년 10월 6일
0

TIL

목록 보기
153/242

KeyboardAvoidingView

react native의 기본 설정은
input 창을 눌렀을 때, 키보드가 올라오는 영역에 input창이 있으면
키보드가 위를 덮어서 사라져버린다

KeyboardAvoidingView 태그를 이용하면
키보드가 올라오는 영역에 대해 유동적인 대응이 가능하고
KeyboardAvoidingView 내부에 TouchableWithoutFeedback 태그를 주어
키보드 외부 영역 onPress 시 키보드 창을 닫는 기능을 수행하게 할 수 있다

import React from 'react';
import { View, KeyboardAvoidingView, TextInput, StyleSheet, Text, Platform, TouchableWithoutFeedback, Button, Keyboard  } from 'react-native';

const KeyboardAvoidingComponent = () => {
  return (
    <KeyboardAvoidingView
      behavior={Platform.OS === "ios" ? "padding" : "height"}
      style={styles.container}
    >
      <TouchableWithoutFeedback onPress={Keyboard.dismiss}>
        <View style={styles.inner}>
          <Text style={styles.header}>Header</Text>
          <TextInput placeholder="Username" style={styles.textInput} />
          <View style={styles.btnContainer}>
            <Button title="Submit" onPress={() => null} />
          </View>
        </View>
      </TouchableWithoutFeedback>
    </KeyboardAvoidingView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1
  },
  inner: {
    padding: 24,
    flex: 1,
    justifyContent: "space-around"
  },
  header: {
    fontSize: 36,
    marginBottom: 48
  },
  textInput: {
    height: 40,
    borderColor: "#000000",
    borderBottomWidth: 1,
    marginBottom: 36
  },
  btnContainer: {
    backgroundColor: "white",
    marginTop: 12
  }
});

export default KeyboardAvoidingComponent;
profile
떠돌이 생활을 하는. 실업자는 아니지만, 부랑 생활을 하는

0개의 댓글