【问题标题】:How to set iOS status bar background color in React Native?如何在 React Native 中设置 iOS 状态栏背景颜色?
【发布时间】:2017-01-10 20:36:32
【问题描述】:

React Native iOS 原生代码中是否有一个地方可以修改以设置 iOS 状态栏背景颜色? RCTRootView.m ?

react native StatusBar component 仅支持 Android 的 backgroundColor。

iOS operating system seems to allow setting status bar backgroundColor

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

【问题讨论】:

标签: ios react-native ios7-statusbar


【解决方案1】:

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

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

【讨论】:

  • 哇!我刚刚在 Android 和 iOS 上进行了测试。完美的!这是一个出色的跨平台解决方案,开箱即用!非常感谢您花时间发布这个。我希望这将有助于许多开发人员。
  • 我尝试对导航栏的其余部分使用&lt;Navigator.NavigationBar/&gt; 组件进行类似修复,但无法使其正常工作(尝试了顶部边框并在其中插入另一个组件)。有什么想法吗?
  • 这不是只有在您的内容不高于屏幕尺寸时才有效吗?如果你必须滚动状态栏背景会被隐藏。
  • @sowmya 更新了解决方案以添加对新版本 Android 的支持。
  • 这是一个很好的解决方案,但是对于有缺口的 iphone,如何实现这一点??
【解决方案2】:

将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 了解更多信息。

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

【讨论】:

  • 理想情况下,将调用StatusBar.setBarStyle() 放在哪里?我尝试将它放在我的根组件的 render() 和 componentWillMount 中,但它使 Xcode 严重崩溃......是否有另一个地方可以很好地进行此调用?谢谢!
  • 好的——我想我可能已经修好了。还有两个我没有提到的问题:1)我正在使用带有 StatusBar 的 NavigatorIOS(我见过的大多数示例都使用通用 Navigator),2)我正在测试 on device 带有发布配置。所以...为了解决这个问题,我从根组件中取出调用并将其移至 NavigatorIOS 的 initialRoute 使用的组件(我们称之为 ToDoList)。在 ToDoList 的 componentDidMount 中,我添加了对 StatusBar.setBarStyle('light-content', false) 的调用,它起作用了!
  • 在一个不使用render()的函数式组件中,把它放到你的App()函数中。如果您的应用程序很轻,并且您希望在状态栏中显示深色文本,请使用 {'dark-content', true}
【解决方案3】:

如果你使用 react-native-navigation,你需要:

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

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

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

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

此示例将您的状态栏转换为浅色文本/按钮/图标颜色。

【讨论】:

  • 您发布的代码仅将状态栏的文本主题设置为亮起。您是如何实现与内容相同的背景的?
  • @Ash 我假设如果标题自动为背景红色,则状态栏将变为红色,因为标题将位于状态栏后面
  • ExceptionsManager.js:179 RCTStatusBarManager 模块要求 Info.plist 中的 UIViewControllerBasedStatusBarAppearance 键设置为 NO
【解决方案4】:

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

【讨论】:

    【解决方案5】:

    您需要自定义它。
    如果您使用来自reac-native 的SafeAreaView,则StatusBar 不是ios 屏幕布局的一部分

    改为使用 react-native-safe-area-context 并对其进行自定义。

    看到这个snack。

    【讨论】:

      【解决方案6】:

      添加到根视图。 (如果有,则超出 SafeAreaView)

      {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
      ...
      

      【讨论】:

        【解决方案7】:
        import {SafeAreaConsumer} from 'react-native-safe-area-context';
        
        <SafeAreaConsumer>
        {(insets)=>(
        <View style={{flex:1}}>
        <View style={{height:insets.top ,backgroundColor :"black"}}/>
        <StatusBar barStyle="light-content"/>
        <View style={{flex:1}}>
          <Text>My Text</Text>
        </View>
        </View>
        )}
        </SafeAreaConsumer>
        

        【讨论】:

          【解决方案8】:

          请务必使用padding 而不是margin。这是我的代码,它在 iOS 中运行良好:

          import { SafeAreaView } from 'react-native'
          import { StatusBar } from 'expo-status-bar'
          // ...
          // ...
          return <SafeAreaView style={{ paddingTop: height }}>
            {/* your content here */}
            <StatusBar style="light" />
          </SafeAreaView>
          

          【讨论】:

            【解决方案9】:

            稍微改变了jmurzy的解决方案,这个应该适用于android和iOS,包括带有notch的iOS:

            import React from 'react'
            import { SafeAreaView, StatusBar, View } from 'react-native'
            import { SafeAreaProvider, useSafeAreaInsets } from 'react-native-safe-area-context'
            
            const App = () => {
                const isDarkMode = true
                return (
                    <SafeAreaProvider>
                        <CustomStatusBar
                            barStyle={isDarkMode ? 'light-content' : 'dark-content'}
                            backgroundColor={'red'}
                        />
                        <SafeAreaView style={{ flex: 1 }}>
            
                            
                        </SafeAreaView>
                    </SafeAreaProvider>
                )
            }
            
            export default App
            
            const CustomStatusBar = ({backgroundColor, ...props}) => {
                const { top } = useSafeAreaInsets()
            
                return (
                    <View style={{ height: (StatusBar.currentHeight || top), backgroundColor }}>
                        <SafeAreaView style={{ backgroundColor }}>
                            <StatusBar translucent backgroundColor={backgroundColor} {...props} />
                        </SafeAreaView>
                    </View>
                )
            }
            

            【讨论】:

              【解决方案10】:

              如果你使用 React Navigation,那真的很简单!

              只需设置headerStyle,另见documentation here。

              类似

              <Stack.Navigator initialRouteName="Home">
                <Stack.Screen
                  name="Home"
                  component={HomeScreen}
                  options={{
                    title: 'my app',
                    headerStyle: {
                      backgroundColor: 'green',
                    },
                  }}
                />
              </Stack.Navigator>
              

              【讨论】:

                【解决方案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>
                    </>
                  )
                }
                

                【讨论】:

                  【解决方案12】:
                   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>
                              )
                          }
                  

                  【讨论】:

                  • 这是一部安卓手机。这个问题是针对iOS。
                  【解决方案13】:

                  我能够更改 iOS 上状态栏的背景颜色,但更新了 SafeAreaView 组件的 backgroundColor 属性。

                  <SafeAreaView style={{backgroundColor: 'blue'}}>
                     // App Content
                  </SafeAreaView>
                  

                  【讨论】:

                    猜你喜欢
                    • 2020-01-26
                    • 1970-01-01
                    • 2019-06-18
                    • 2013-10-11
                    • 2015-06-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2018-04-10
                    • 2016-10-16
                    相关资源
                    最近更新 更多