question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Keyboard height wrong with notches

See original GitHub issue

The height of the keyboard as reported by the keyboardDidShow event is wrong when a notch is present.

React Native version: 0.59.8 (Expo 34.0.0) Tested on Android

Steps To Reproduce

  1. Listen for keyboardDidShow events
  2. Inspect the event details (note the height and screenY properties in particular)
  3. Compare these values to Dimensions.get('screen') and Dimensions.get('window')

Expected behaviour

The height property of the keyboardDidShow event reflects the correct height of the keyboard

Actual behaviour

The height that is returned is slightly smaller than the expected value: the difference seems to be the height of the notch. I assume that the screenY property (correctly) reflects the offset from the top of the screen (upper side of the notch), but the height is calculated using the window dimensions (everything under the notch). For phones that don’t have a notch, the top of the screen and window are the same, so the height is correct. However, on phones with a notch, the top of the window is the bottom of the notch and as a result, the height is too small.

Example with actual numbers

Pixel 3 (no notch):

  • screen height: 785
  • window height: 737 (excludes on-screen system controls at the bottom of the screen, but includes status bar)
  • keyboard screenY: 447
  • keyboard height: 289

Pixel 3 XL (with notch):

  • screen height: 845
  • window height: 748 (excludes system controls and notch)
  • keyboard screenY: 515
  • keyboard height: 233

As you can see, screenY + keyboardHeight = windowHeight in both cases. As screenY is a screen coordinate, not window, this should be equal to screenHeight instead.

Snack: https://snack.expo.io/Sk_yXw9Sr

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:11

github_iconTop GitHub Comments

1reaction
vandrieucommented, Oct 8, 2019

@MichalKrakow Got it working!

Before:

My App:

export default class App extends Component {
   render() {
      return (
         <MySwitchNavigator />
      )
   }
}

My component with keyboard:

const keyboardHeight = event.endCoordinates.height;

After:

My App:

export default class App extends Component {
   render() {
      return (
         <View
            style={{ flex: 1 }}
            onLayout={({ nativeEvent }) => MyConstants.realScreenHeight = nativeEvent.layout.height}
         >
         <MySwitchNavigator />
         </View>
      )
   }
}

My component with keyboard:

const keyboardHeight = MyConstants.realScreenHeight - event.endCoordinates.screenY;

Thanks!

1reaction
MichalKrakowcommented, Oct 8, 2019

@vandrieu (im using expo 30.0) Basically you cannot rely on returned keyboard height and dimensions.get(‘window’). So instead use container wrapping whole screen layout and get it’s height on ‘onLayout’ event - this is your screen height. Then ‘keyboardDidShow’ event returns correct endCoordinates.screenY value. so you get the top edge of keyboard. Subtract those and you have keyboard height. Double check if your container gets whole sceen (statusbar etc…) cause if not you have to add this in order to get correct height.

Read more comments on GitHub >

github_iconTop Results From Across the Web

iPhoneX and iPhone 8 keyboard height are different
The keyboard height for both iPhone X and iPhone 8 should be correct. I can only guess that maybe you have ...
Read more >
Developers - Keyboard height wrong with notches - - Bountysource
Coming soon: A brand new website interface for an even better experience!
Read more >
Getting the keyboard height | Before I forget...
We have the keyboard height. The person I was answering on StackOverflow was trying to display a view directly above the keyboard, and...
Read more >
Keyboard hides input field on iPhone device - OutSystems
I have solved this problem with a blank container with a height of 600px at the bottom of the screen. Create a local...
Read more >
Using the new Backcheck Height / Line Jig
At this point the keyboard should have been leveled and dipped to the final specifications. Locate the tool so that the point of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found