React Native IOS Status Bar background

匿名 (未验证) 提交于 2019-12-03 01:33:01

问题:

Since Applying the backgroundColor props to StatusBar component doesn't get applied On IOS. I need to set the background colour of SafeAreaView to get the effect i want, it works fine but on iPhone X it will have that same colour at the bottom of the screen. How can I solve this issue?

回答1:

React-Native does not support background color change of StatusBar on iOS platform but on Android platform, it's ok (https://facebook.github.io/react-native/docs/statusbar#backgroundcolor). I wrote a work around for your problem. You can use it safely

import React, {Component} from "react"; import {StyleSheet, StatusBar, View, Platform} from "react-native";  const STATUS_BAR_HEIGHT = Platform.OS === 'ios' ? 20 : StatusBar.currentHeight;  function StatusBarPlaceHolder() {     return (         <View style={{             width: "100%",             height: STATUS_BAR_HEIGHT,             backgroundColor: "blue"         }}>             <StatusBar                 barStyle="light-content"             />         </View>     ); }  class App extends Component {     render() {         return (             <View style={{flex: 1}}>                 <StatusBarPlaceHolder/>                 ...YourContent             </View>         );     } }  export default App; 

For SafeAreaView:

import React, {Component} from "react"; import {StyleSheet, StatusBar, View, Platform} from "react-native"; import SafeAreaView from "react-native-safe-area-view"; //You can also use react-navigation package for SafeAreaView with forceInset.  const STATUS_BAR_HEIGHT = Platform.OS === 'ios' ? 20 : StatusBar.currentHeight;  function StatusBarPlaceHolder() {     return (         <View style={{             width: "100%",             height: STATUS_BAR_HEIGHT,             backgroundColor: "blue"         }}>             <StatusBar                 barStyle="light-content"             />         </View>     ); }  class App extends Component {     render() {         return (             <SafeAreaView style={{flex:1}}                 forceInset={{top: 'never'}}>                 <StatusBarPlaceHolder/>                 ...YourContent             </SafeAreaView>         );     } }  export default App; 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!