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

新手上路,请多包涵
阅读 1.4k
2 个回答

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

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

const MyStatusBar = ({backgroundColor, ...props}) => (
  <View style={[styles.statusBar, { backgroundColor }]}>
    <SafeAreaView>
      <StatusBar translucent backgroundColor={backgroundColor} {...props} />
    </SafeAreaView>
  </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 = 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);

模拟器

也许代码中不清楚,但诀窍是使用 StatusBar ,它适用于 Android,并创建一个“假”状态栏(一个 ViewbackgroundColor ) 对于 IOS。

原文由 jmurzy 发布,翻译遵循 CC BY-SA 4.0 许可协议

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/图标为白色。

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

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

除此之外:不,您必须点击您提供的链接。

原文由 dv3 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题