React Native KeyboardAvoidingView(キーボード回避ビュー)

概要: このチュートリアルでは、React NativeのKeyboardAvoidingViewコンポーネントを使用して、アプリのユーザーエクスペリエンスを向上させる方法を学習します。

React Native KeyboardAvoidingViewコンポーネントの概要

ユーザーがTextInputにフォーカスすると、仮想キーボードが表示され、入力フィールドや他のUIコンポーネントが隠れてしまう場合があります。 これにより、ユーザーはフォームに正しくデータを入力したり、表示したりすることができなくなるため、ユーザーエクスペリエンスが低下する可能性があります。

たとえば、次のアプリでは、仮想キーボードによってiOSでTextInputコンポーネントが完全に隠れてしまいます。

import { StyleSheet, Text, View, SafeAreaView, TextInput } from 'react-native'

const App = () => {
    return (
        <SafeAreaView style={styles.container}>
            <Text style={styles.title}>JavaScriptTutorial.net</Text>
            <View style={styles.field}>
                <Text style={styles.label}>Name:</Text>
                <TextInput placeholder="Enter your name" style={styles.input} />
            </View>
        </SafeAreaView >
    )
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        padding: 16,
    },
    title: {
        fontSize: 28,
        textAlign: 'center',
        marginBottom: 16,
    },
    field: {
        marginTop: 'auto',
    },
    label: {
        marginBottom: 8,
        fontSize: 16,
    },
    input: {
        height: 40,
        marginBottom: 16,
        borderWidth: 1,
        borderColor: '#ccc',
        borderRadius: 8,
        padding: 8,
        fontSize: 16,
    },

});

export default App;Code language: JavaScript (javascript)

iOSに切り替えて、TextInputにフォーカスを移動すると、問題が発生することがわかります。

この問題を解決するには、KeyboardAvoidingViewコンポーネントを使用して、キーボードが表示されてもTextInputが表示されたままになるようにします。

KeyboardAvoidingViewコンポーネントは、仮想キーボードが表示されている間、表示されたままになるように、仮想キーボードの_高さ_、_位置_、または_下部パディング_を自動的に調整します。

KeyboardAvoidingViewコンポーネントを使用するには

まず、react-nativeライブラリからKeyboardAvoidingViewをインポートします。

import { KeyboardAvoidingView } from 'react-native';Code language: JavaScript (javascript)

次に、仮想キーボードが表示されている間、表示されたままにしたい領域をKeyboardAvoidingViewコンポーネントでラップします。

<KeyboardAvoidingView>
   {/* UI components */}
</KeyboardAvoidingView>Code language: JavaScript (javascript)

KeyboardAvoidingViewには、behaviorという重要なプロパティがあり、仮想キーボードの表示に対する反応方法を定義します。 このプロパティを使用して、iOSやAndroidなどの異なるプラットフォームに基づいて、KeyboardAvoidingViewの動作を指定できます。

React Nativeチームからの推奨事項は次のとおりです。

<KeyboardAvoidingView behavior={Platform.OS === "ios" ? "padding" : "height"}>
</KeyboardAvoidingView>Code language: HTML, XML (xml)

アプリの問題を解決するには、フォームをKeyboardAvoidingViewで次のようにラップします。

import { StyleSheet, Text, View, SafeAreaView, TextInput, KeyboardAvoidingView, Platform } from 'react-native'

const App = () => {
    return (
        <SafeAreaView style={styles.container}>
            <KeyboardAvoidingView behavior={Platform.OS === "ios" ? "padding" : "height"} style={styles.container}>
                <Text style={styles.title}>JavaScriptTutorial.net</Text>
                <View style={styles.field}>
                    <Text style={styles.label}>Name:</Text>
                    <TextInput placeholder="Enter your name" style={styles.input} />
                </View>
            </KeyboardAvoidingView>
        </SafeAreaView >
    )
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        padding: 16,
    },
    title: {
        fontSize: 28,
        textAlign: 'center',
        marginBottom: 16,
    },
    field: {
        marginTop: 'auto',
    },
    label: {
        marginBottom: 8,
        fontSize: 16,
    },
    input: {
        height: 40,
        marginBottom: 16,
        borderWidth: 1,
        borderColor: '#ccc',
        borderRadius: 8,
        padding: 8,
        fontSize: 16,
    },

});

export default App; Code language: JavaScript (javascript)

iOSに切り替えてTextInputにフォーカスすると、仮想キーボードが表示され、TextInputコンポーネントが上に押し上げられ、データ入力のためにTextInputが表示されたままになります。

まとめ

  • KeyboardAvoidingViewコンポーネントを使用してフォーム要素をラップし、仮想キーボードが表示されたときにフォームが表示されたままになるようにします。
このチュートリアルは役に立ちましたか?