Isaac
Isaac

Reputation: 12874

How to deal with safeAreaView + React Navigation?

enter image description here

First screenshot was without applying SafeAreaView and second screenshot is applying SafeAreaView

enter image description here

As shown clearly that Stack header seems bulky as compare to previously. Is there anyway where we can apply SafeAreaView to only bottom part?

Upvotes: 11

Views: 34033

Answers (5)

y4h2
y4h2

Reputation: 3383

In react navigation 6.x, it recommends to use useSafeAreaInsets hook (as SafeAreaView only supports iOS 10+).

  1. wrap the NavigationContainer with SafeAreProvider
  2. use useSafeAreaInsets in top layer component style's padding

Notice the padding block in the example

        paddingTop: insets.top,
        paddingBottom: insets.bottom,
        paddingLeft: insets.left,
        paddingRight: insets.right,
import {
  SafeAreaProvider,
  useSafeAreaInsets,
} from 'react-native-safe-area-context';

function Demo() {
  const insets = useSafeAreaInsets();

  return (
    <View
      style={{
        flex: 1,
        justifyContent: 'space-between',
        alignItems: 'center',

        // Paddings to handle safe area
        paddingTop: insets.top,
        paddingBottom: insets.bottom,
        paddingLeft: insets.left,
        paddingRight: insets.right,
      }}
    >
      <Text>This is top text.</Text>
      <Text>This is bottom text.</Text>
    </View>
  );
}

export default function App() {
  return (
    <SafeAreaProvider>
      <NavigationContainer>{/*(...) */}</NavigationContainer>
    </SafeAreaProvider>
  );
}

Reference: https://reactnavigation.org/docs/handling-safe-area/

Upvotes: 0

Sushil Kumar
Sushil Kumar

Reputation: 179

If you want to ignore bulky padding at the top of safeAreaView, pass edges props to control padding.

<SafeAreaView style={styles.container} edges={['top', 'left', 'right']}>
  <Text style={styles.paragraph}>This is top text.</Text>
  <Text style={styles.paragraph}>This is bottom text.</Text>
</SafeAreaView>

read more about it here

Upvotes: 3

Yilmaz
Yilmaz

Reputation: 49293

react-native also has "SafeAreaView" but this works only in ios.

import { SafeAreaView } from "react-native";

so set the SetAreaView with android, you use StatusBar which works only in android and its currentHeight is 24.

  <SafeAreaView style={{ flex: 1, marginTop: StatusBar.currentHeight }}>
    <View style={{ padding: 16, backgroundColor: "green" }}>
      <Text>Page content</Text>
    </View>
  </SafeAreaView>

Upvotes: 0

wobsoriano
wobsoriano

Reputation: 13434

For React Navigation v5, there is no SafeAreaView exported. The recommended way is to use react-native-safe-area-context.

Read more: React Navigation v5.x - Supporting safe areas.

Example

import { SafeAreaProvider, SafeAreaView } from 'react-native-safe-area-context';

function Demo() {
  return (
    <SafeAreaView
      style={{ flex: 1, justifyContent: 'space-between', alignItems: 'center' }}
    >
      <Text>This is top text.</Text>
      <Text>This is bottom text.</Text>
    </SafeAreaView>
  );
}

export default function App() {
  return (
    <SafeAreaProvider>
      <NavigationContainer>{/*(...) */}</NavigationContainer>
    </SafeAreaProvider>
  );
}

Upvotes: 26

Isaac
Isaac

Reputation: 12874

Instead of using SafeAreaView from React-Native, use SafeAreaView from react-navigation as below:

import { SafeAreaView } from 'react-navigation';

Then you can use prop forceInset to customize the padding, which in your case,

<SafeAreaView style={styles.safeArea} forceInset={{ top: 'never' }}>

For more information, check out the iPhone X support by react-navigation

Upvotes: 19

Related Questions