How to set iOS status bar background color in React Native?

僤鯓⒐⒋嵵緔 提交于 2019-11-27 10:17:52

iOS doesn't have a concept of a status bar bg. Here's how you'd achieve this in a cross-platform way:

import React, {
  Component,
} from 'react';
import {
  AppRegistry,
  StyleSheet,
  View,
  StatusBar,
  Platform,
} from 'react-native';

const MyStatusBar = ({backgroundColor, ...props}) => (
  <View style={[styles.statusBar, { backgroundColor }]}>
    <StatusBar translucent backgroundColor={backgroundColor} {...props} />
  </View>
);

class DarkTheme extends Component {
  render() {
    return (
      <View style={styles.container}>
        <MyStatusBar backgroundColor="#5E8D48" barStyle="light-content" />
        <View style={styles.appBar} />
        <View style={styles.content} />
      </View>
    );
  }
}

const STATUSBAR_HEIGHT = Platform.OS === 'ios' ? 20 : StatusBar.currentHeight;
const APPBAR_HEIGHT = Platform.OS === 'ios' ? 44 : 56;

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  statusBar: {
    height: STATUSBAR_HEIGHT,
  },
  appBar: {
    backgroundColor:'#79B45D',
    height: APPBAR_HEIGHT,
  },
  content: {
    flex: 1,
    backgroundColor: '#33373B',
  },
});

AppRegistry.registerComponent('App', () => DarkTheme);

dv3

Add import { StatusBar } from 'react-native'; to the top of your app.js and then add StatusBar.setBarStyle('light-content', true); as the first line in your render() to change the status bar text/icons to white.

The other color options are 'default' and 'dark-content'.

Refer to https://facebook.github.io/react-native/docs/statusbar.html for further info.

Other than that: no, you would have to follow the link you provided.

Luis Antonio Pestana

If you are using react-native-navigation, you need to:

1-) Add this to your info.plist file:

<key>UIViewControllerBasedStatusBarAppearance</key>
<string>YES</string>

2-) At first line of your render() function, eg:

  render(){
    this.props.navigator.setStyle({
      statusBarTextColorScheme: 'light'
    });
    return (
      <Login navigator={this.props.navigator}></Login>
    );
  }

This example will transform your status bar to light text/buttons/icons color.

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