How to get a height of a Keyboard in React-Native?

后端 未结 4 1052
一整个雨季
一整个雨季 2020-12-08 03:49

I am using React-Navigation in my app and the app consists of StackNavigator with multiple screens, some screens of which have TextInput with autoFocus={true}

相关标签:
4条回答
  • 2020-12-08 04:40

    For those of you still looking for an answer to this now you can use hooks.

    import { useKeyboard } from '@react-native-community/hooks'
    
    //Then keyboard like this 
    
    const keyboard = useKeyboard()
    
    console.log('keyboard isKeyboardShow: ', keyboard.keyboardShown)
    console.log('keyboard keyboardHeight: ', keyboard.keyboardHeight)
    
    0 讨论(0)
  • 2020-12-08 04:45

    Just would like to add to the above answers, that using keyboardWillShow and keyboardWillHide rather than keyboardDidShow and keyboardDidHide will look much better. It just runs sooner and hence, looks smoother.

    0 讨论(0)
  • 2020-12-08 04:46

    This is what I did:

    If the app has "Authorization / Log-in / Sign-up screen" then:

    1. In componentWillMount add KeyboardListeners as explained here:

      this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this._keyboardDidShow);
      this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this._keyboardDidHide);
      
    2. Add autoFocus to e-mail / phone number / any other "first" TextInput on the page, so that when the screen loads, the Keyboard pops-up.

    3. In _keyboardDidShow function, that is used as a KeyboardListener, do the follows:

      _keyboardDidShow(e) {
          this.props.navigation.setParams({
              keyboardHeight: e.endCoordinates.height,
              normalHeight: Dimensions.get('window').height, 
              shortHeight: Dimensions.get('window').height - e.endCoordinates.height, 
          }); 
      }
      

      Dimensions is an API of React-Native, do not forget to import it just like you import any React-Native component.

    4. After that, while redirecting to the next page, pass these params and do not forget to keep on passing them to other screens in order not to lose this data:

      this.props.navigation.navigate('pageName', { params: this.props.navigation.state.params });
      
    0 讨论(0)
  • 2020-12-08 04:51

    I also needed a hook for it, so that is how I get the height of the keyboard (largely inspired by the other answer), the code example is in TypeScript:

    import { useEffect, useState } from 'react';
    import { Keyboard, KeyboardEvent } from 'react-native';
    
    export const useKeyboard = (): [number] => {
      const [keyboardHeight, setKeyboardHeight] = useState(0);
    
      function onKeyboardDidShow(e: KeyboardEvent): void {
        setKeyboardHeight(e.endCoordinates.height);
      }
    
      function onKeyboardDidHide(): void {
        setKeyboardHeight(0);
      }
    
      useEffect(() => {
        Keyboard.addListener('keyboardDidShow', onKeyboardDidShow);
        Keyboard.addListener('keyboardDidHide', onKeyboardDidHide);
        return (): void => {
          Keyboard.removeListener('keyboardDidShow', onKeyboardDidShow);
          Keyboard.removeListener('keyboardDidHide', onKeyboardDidHide);
        };
      }, []);
    
      return [keyboardHeight];
    };
    

    then in your component:

      const [keyboardHeight] = useKeyboard();
      console.log(keyboardHeight);
    
    0 讨论(0)
提交回复
热议问题