react-native-safe-area
React Native module to handle safe area insets natively for iOS 11 or later.

Installation
1. Install library from npm
npm install --save react-native-safe-area
2. Link native code
You can link native code in the way you prefer:
CocoaPods
Add line to your project target section in your Podfile:
+ pod 'react-native-safe-area', path: '../node_modules/react-native-safe-area'
react-native link
Run command below:
react-native link react-native-safe-area
Usage
Work with views
Use withSafeArea
to apply safe area insets to views automatically.
import { withSafeArea } from 'react-native-safe-area'
withSafeArea(component[, applyTo][, direction])
A higher-order component which applies safe area insets automatically to the wrapped component.
- component - Wrapped component.
- applyTo - (Optional) Specify property to apply safe area insets.
margin
- (Default) style.margin
.
padding
- style.padding
.
contentInset
- contentInset
and contentOffset
for scroll views.
- direction - (Optional) Specify direction to apply safe area insets.
horizontal
- Apply to left and right.
vertical
- Apply to top and bottom.
both
- (Default) horizontal
+ vertical
.
Simple view example
const SafeAreaView = withSafeArea(View, 'margin', 'both')
class App extends Component<{}> {
render() {
return (
<SafeAreaView>
<View />
</SafeAreaView>
)
}
}
ScrollView example
const SafeAreaScrollView = withSafeArea(ScrollView, 'contentInset', 'vertical')
class App extends Component<{}> {
render() {
return (
<SafeAreaScrollView>
<View />
</SafeAreaScrollView>
)
}
}
You can also apply safe area insets to FlatList and SectionList.
Handle safe area manually
import SafeArea from 'react-native-safe-area'
If you want to use SafeAreaInsets
type, you can import it like below:
import SafeArea, { type SafeAreaInsets } from 'react-native-safe-area'
Retrieve safe area insets for root view
SafeArea.getSafeAreaInsetsForRootView()
.then((result) => {
console.log(result)
})
Handle safe area insets changed event
class App extends Component<{}> {
onSafeAreaInsetsForRootViewChange = this.onSafeAreaInsetsForRootViewChange.bind(this)
componentDidMount() {
SafeArea.addEventListener('safeAreaInsetsForRootViewDidChange', this.onSafeAreaInsetsForRootViewChange)
}
componentWillUnmount() {
SafeArea.removeEventListener('safeAreaInsetsForRootViewDidChange', this.onSafeAreaInsetsForRootViewChange)
}
onSafeAreaInsetsForRootViewChange(result) {
console.log(result)
}
}
Examples
A simple example project is here.