如何在React Native中设置iOS状态栏背景颜色?

Ed *_*ain 56 ios ios7-statusbar react-native

在本机iOS本机代码中是否有一个地方可以修改以设置iOS状态栏backgroundColor?RCTRootView.m?

反应原生状态栏组件只支持的backgroundColor为仅适用于Android.

iOS操作系统似乎允许设置状态栏的backgroundColor

我的目标是使颜色更深的状态栏颜色. 例

jmu*_*rzy 150

iOS没有状态栏bg的概念.以下是您如何以跨平台的方式实现这一目标:

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);
Run Code Online (Sandbox Code Playgroud)

模拟器

  • 哇!我刚刚在Android和iOS上测试过.完善!这是一个出色的跨平台解决方案,开箱即用!非常感谢您花时间发布此内容.我希望这对许多开发人员有所帮助. (9认同)
  • 这是一个很好的解决方案,但是对于有缺口的iPhone如何实现这一点? (3认同)
  • 这不是只有当您的内容不高于屏幕尺寸时才有效吗?如果必须滚动状态栏背景就会隐藏。 (2认同)

dv3*_*dv3 37

添加import { StatusBar } from 'react-native';到您的顶部,app.js然后添加StatusBar.setBarStyle('light-content', true);为您的第一行,render()将状态栏文本/图标更改为白色.

其他颜色选项是'default''dark-content'.

有关详细信息,请参阅https://facebook.github.io/react-native/docs/statusbar.html.

除此之外:不,你必须按照你提供的链接.


Ahm*_*ber 15

你需要定制它。
StatusBar 不是 ios 屏幕布局的一部分,否则如果您使用SafeAreaViewreac-native

相反,使用react-native-safe-area-context并自定义它。

看到这个小吃

在此输入图像描述


小智 11

这适用于我的react-native-safe-area-context版本3.3.2

import { StatusBar } from "react-native"
import { SafeAreaProvider, SafeAreaView } from "react-native-safe-area-context"

const App = () => {

  const theme = ... get your theme

  return (
    <>
      <StatusBar
        backgroundColor={theme === "light" ? "#fff" : "#000"}
        barStyle={theme === "light" ? "dark-content" : "light-content"}
      />
      <SafeAreaProvider>
        <SafeAreaView
          style={{
            flex: 1,
            backgroundColor: theme === "light" ? "#fff" : "#000",
          }}
        >  

          // stuff goes here

        </SafeAreaView>
      </SafeAreaProvider>
    </>
  )
}
Run Code Online (Sandbox Code Playgroud)


Lui*_*ana 5

如果您使用的是 react-native-navigation,则需要:

1-) 将此添加到您的 info.plist 文件中:

<key>UIViewControllerBasedStatusBarAppearance</key>
<string>YES</string>
Run Code Online (Sandbox Code Playgroud)

2-) 在render()函数的第一行,例如:

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

此示例将您的状态栏转换为浅色文本/按钮/图标颜色。 在此处输入图片说明


Tal*_*ved 5

在 react-native 中设置 iOS 和 Android 状态栏背景颜色

    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;
Run Code Online (Sandbox Code Playgroud)