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

后端 未结 6 1123
失恋的感觉
失恋的感觉 2020-11-28 18:43

Is there a single place in the react native iOS native code that I could modify to set iOS statusbar backgroundColor? RCTRootView.m ?

The react native StatusBar comp

相关标签:
6条回答
  • 2020-11-28 18:58

    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);
    

    0 讨论(0)
  • 2020-11-28 18:58

    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.

    0 讨论(0)
  • 2020-11-28 19:00

    set iOS & Android statusbar backgroundColor in react-native

        import React, { Component } from 'react';
        import { Platform, StyleSheet, View, StatusBar } from 'react-native';
        import Constants from 'expo-constants';
    
    
        class Statusbar extends Component {
    
            render() {
                return (
                    <View style={styles.StatusBar}>
                        <StatusBar translucent barStyle="light-content" />
                    </View>
                );
            }
        }
    
        const styles = StyleSheet.create({
            StatusBar: {
                height: Constants.statusBarHeight,
                backgroundColor: 'rgba(22,7,92,1)'
            }
        });
    
        export default Statusbar;
    
    0 讨论(0)
  • 2020-11-28 19:13
     render() {
            let { email, password, isLoading } = this.state
            return (
                <View style={{ flex: 1, }}>
                    <StatusBar
                        translucent
                        barStyle="light-content"
                        //  backgroundColor="rgba(0, 0, 0, 0.251)"
                        backgroundColor='magenta'
                     />
                </View>
                )
            }
    

    0 讨论(0)
  • 2020-11-28 19:22

    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.

    0 讨论(0)
  • 2020-11-28 19:22

    Add to root view. (Out of SafeAreaView if has)

    {Platform.OS === 'ios' &&
     <View style={{
         width: "100%",
         height: 100, // For all devices, even X, XS Max
         position: 'absolute',
         top: 0,
         left: 0,
         backgroundColor: "red"}}
    />}
    // App screen
    ...
    
    0 讨论(0)
提交回复
热议问题