How to prevent layout from overlapping with iOS status bar

前端 未结 8 2106
礼貌的吻别
礼貌的吻别 2020-12-24 04:45

I am working on tutorial for React Native navigation. I found out that all layout starts loading from top of screen instead of below of the status bar. This causes most layo

8条回答
  •  旧时难觅i
    2020-12-24 05:00

    I tried a more simple way for this.

    We can get the height of Status Bar on android and use SafeAreaView along with it to make the code work on both platforms.

    import { SafeAreaView, StatusBar, Platform } from 'react-native';
    

    If we log out Platform.OS and StatusBar.currentHeight we get the logs,

    console.log('Height on: ', Platform.OS, StatusBar.currentHeight);
    

    Height on: android 24 and Height on: android 24

    We can now optionally add margin/padding to our container view using

    paddingTop: Platform.OS === "android" ? StatusBar.currentHeight : 0
    

    The final code in App.js is below:

    export default class App extends React.Component {
      render() {
        return (
          
            
              Hello World
            
          
        );
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        backgroundColor: "#fff",
        paddingTop: Platform.OS === "android" ? StatusBar.currentHeight : 0
      }
    });
    

提交回复
热议问题