24

For Android I know I can use StatusBar.currentHeight but I'm not sure how to do so for iOS.

The answer to how to retrieve the size in Swift(native) has already been answered but I need this in a react native app.

jasan
  • 11,475
  • 22
  • 57
  • 97

3 Answers3

23

If you're using Expo you can use Constants.statusBarHeight.

import Constants from 'expo-constants';
const statusBarHeight = Constants.statusBarHeight;

If you're using Vanilla React Native with React Navigation you can use the following:

import { useSafeAreaInsets } from 'react-native-safe-area-context';
const insets = useSafeAreaInsets();
const statusBarHeight = insets.top;

See: https://reactnavigation.org/docs/handling-safe-area/#use-the-hook-for-more-control

Sample Code:

import * as React from 'react';
import { Text, View, StatusBar } from 'react-native';
import Constants from 'expo-constants';
import { useSafeAreaInsets, SafeAreaProvider } from 'react-native-safe-area-context';

export default function App() {
  return (
    <SafeAreaProvider>
      <ChildScreen />
    </SafeAreaProvider>
  );
}

function ChildScreen() {
  const insets = useSafeAreaInsets();
  
  return (
    <View style={{ flex: 1, justifyContent: 'center'}}>
      <Text>
        {insets.top}
      </Text>
      <Text>
        {Constants.statusBarHeight}
      </Text>
      <Text>
        {StatusBar.currentHeight ?? 'N/A'}
      </Text>
    </View>
  );
}

Output:

Samsung Galaxy S10 5G iPhone 8 Plus iPhone 11 Pro Max Web
insets.top 39.71428680419922 20 44 0
Constants.statusBarHeight 39 20 44 0
StatusBar.currentHeight ?? 'N/A' 39.42856979370117 N/A N/A N/A

Live code: https://snack.expo.dev/@dcangulo/statusbarheight

dcangulo
  • 1,888
  • 1
  • 16
  • 48
4

You can use React Navigation that already have support of iPhone X.

Even if you don't want use this library because of some reason - you still can read source code to copy implementation in your code

  • React Navigation does support notch screens. Here's the [detail document](https://reactnavigation.org/docs/en/handling-iphonex.html). It says the device info can be retrieved by [react-native-device-info](https://github.com/react-native-community/react-native-device-info) – yancaico Apr 29 '19 at 09:00
3

You can use this package, it has very good documentation. react-native-status-bar-height

Cyber
  • 164
  • 7