Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you prevent the bottom area in a React Native SafeAreaView from overlapping over the content?

I'm implementing a <SafeAreaView> on my React Native app. Most of my screens are in a ScrollView. When I add the <SafeAreaView>, it obstructs the content. While I want this bottom area to be "safe", I'd like the user to be able to see the content behind it, otherwise, the space is wasted.

How do I implement a "transparent" safe area?

Simplified example:

class ExampleScreen extends Component {
  render() {
    return (
      <SafeAreaView>
        <Scrollview>
          <Text>Example</Text>
          <Text>Example</Text>
          <Text>Example</Text>
          (etc)
        </Scrollview>
      </SafeAreaView>
    );
  }
}

Output:

Desired Output:

like image 813
Dan Leveille Avatar asked Mar 20 '18 06:03

Dan Leveille


People also ask

What is safeareaview in React Native?

In React native, the main goal of SafeAreaView component is to render content within the safe area boundaries of a device. All smartphones nowadays come with an edge-to-edge display. But smartphones must have some space left for the front cameras. Therefore all smartphones come with a notch or punch hole on the screen to fit the camera inside.

What is the use of safeareaview component?

But SafeAreaView component takes away that headache from you and makes sure everything works fine on a device. Every component you write in SafeAreaView will render within the safe area boundaries of a device.

What is React Native Content rendering?

In simple words, it is the area where you don’t have to draw your imagination and have to keep it safe for native use. By default when you make a React Native App you have seen that it renders the content on the status bar too

What is the “safe area” in iOS?

For those who have no idea about the safe area, In iOS 7 Apple introduced the top layout guide and bottom layout guide properties which describes a screen area that isn’t covered by any content (status bar, navigation bar, toolbar, tab bar, etc.) but In iOS 11 Apple has deprecated these properties and introduced the safe area.


3 Answers

In most you do not want to have your ScrollView/FlatList have as a descendant of a SafeAreaView. Instead you only want to wrap your Header and TabBar into a SafeAreaView. Some examples:

Instead of this (WRONG EXAMPLE)

<SafeAreaView>
  <Header />
  <ScrollView>
     <Content />
  </ScrollView>
</SafeAreaView>

you only wrap the header

<View>
   <SafeAreaView>
      <Header />
   </SafeAreaView>
   <ScrollView>
     <Content />
   </ScrollView>
</View>

Also even if you do not really have a Header, you only want to avoid drawing behind the status bar, you can use the SafeAreaView as padding.

<View>
   <SafeAreaView /> // <- Now anything after this gonna be rendered after the safe zone
   <Content />
</View>
like image 92
Istvan Orban Avatar answered Oct 19 '22 19:10

Istvan Orban


Maybe this late answer but you can easily use

 class ExampleScreen extends Component {
  render() {
    return (
      <SafeAreaView edges={['right', 'left', 'top']} >
        <Scrollview>
          <Text>Example</Text>
          <Text>Example</Text>
          <Text>Example</Text>
          (etc)
        </Scrollview>
      </SafeAreaView>
    );
  }
}
like image 21
anie Avatar answered Oct 19 '22 19:10

anie


You could try react-navigation's SafeAreaView. Just set it's forceInset prop to { bottom: 'never' } and you'll see it behaves as your expectation.

Example: https://github.com/react-navigation/react-navigation/blob/master/examples/SafeAreaExample/App.js

like image 2
razor1895 Avatar answered Oct 19 '22 19:10

razor1895