Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-native-safe-area-view

react-community445.2kMITdeprecated1.1.1TypeScript support: included

Package has been replaced by react-native-safe-area-context: https://www.npmjs.com/package/react-native-safe-area-context

Add padding to your views to account for notches, home indicators, status bar, and possibly other future things.

react-native, iPhoneX, SafeAreaView, safe area, notch

readme

react-native-safe-area-view

This library provides automatic padding when a view intersects with a safe area (notch, status bar, home indicator).

Installation

In the Expo managed workflow:

expo install react-native-safe-area-view react-native-safe-area-context

In bare React Native projects:

yarn add react-native-safe-area-view react-native-safe-area-context

Next, you need to link react-native-safe-area-context. If you are using autolinking, run pod install again. If not, follow these instructions. Then re-build your app binary.

Usage

First you need to wrap the root of your app with the SafeAreaProvider.

import * as React from 'react';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import MyAwesomeApp from './src/MyAwesomeApp';

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

Now you can wrap components that touch any edge of the screen with a SafeAreaView.

import SafeAreaView from 'react-native-safe-area-view';

export default function MyAwesomeApp() {
  return (
    <SafeAreaView style={{ flex: 1 }}>
      <View style={{ flex: 1 }}>
        <Text>
          Look, I'm safe! Not under a status bar or notch or home indicator or
          anything! Very cool
        </Text>
      </View>
    </SafeAreaView>
  );
}

forceInset

Sometimes you will observe unexpected behavior and jank because SafeAreaView uses onLayout then calls measureInWindow on the view. If you know your view will touch certain edges, use forceInset to force it to apply the inset padding on the view.

<SafeAreaView forceInset={{ top: 'always' }}>
  <View>
    <Text>Yeah, I'm safe too!</Text>
  </View>
</SafeAreaView>

forceInset takes an object with the keys top | bottom | left | right | vertical | horizontal and the values 'always' | 'never'. Or you can override the padding altogether by passing an integer.

Accessing safe area inset values

Sometimes it's useful to know what the insets are for the top, right, bottom, and left of the screen. See the documentation for react-native-safe-area-context for more information.

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

[1.1.0]

Added

[1.0.0]

First release that handles safe area insets properly on iOS and Android!

Removed

  • getInset has been removed. Use the react-native-safe-area-context API directly instead.
  • withSafeArea has been removed. You may want to re-implement this yourself for ease of migration if you used an old version. Once again, use the react-native-safe-area-context APIs directly instead.