Search code examples
iosreactjsreact-nativereact-native-ios

How to know the useful height of an iOS device in React Native?


In some very specific cases I need to set the height of a View to the full height of the device useful area (without using flex).

I was using a hardcoded "notch height" to calculate this useful height but I just discovered that the notch can have different heights depending on the device. (3 points of difference between iPhone XS and iPhone XS Max).

Is there a way to know the useful height of a device with notch and safe area?


Solution

  • You can use the react-native-safe-area. it provides function to Get safe area inset top, bottom, left, right.

    import SafeArea, { type SafeAreaInsets } from 'react-native-safe-area'
    
    //Retrieve safe area insets for root view
    
    SafeArea.getSafeAreaInsetsForRootView()
    .then((result) => {
       console.log(result)
       // { safeAreaInsets: { top: 44, left: 0, bottom: 34, right: 0 } }
    })