我的应用程序背景是黑色的,但在iOS 7中,状态栏变成了透明的。所以我什么也看不见,只有角落里的绿色电池指示灯。如何将状态栏文本颜色改为白色,就像在主屏幕上一样?


当前回答

我做了一些不同的事情,这对我很有效。

在没有更改代码的情况下,我像这样配置了.plist文件:

基于控制器的状态栏外观>否 状态栏样式> UIStatusBarStyleLightContent(简单字符串)

我希望这能有所帮助。

edit

对于每个视图控制器,我改变“状态栏”的模拟度量属性,在故事板中,从“推断”到“轻内容”

其他回答

如果你的UIViewController在UINavigationController中,你必须设置BarStyle:

-[UINavigationBar setBarStyle:UIBarStyleBlack]

原始答案在这里

https://devforums.apple.com/message/844264#844264

如果你想将它设置为任何颜色,请使用下面的代码。

id statusBarWindow = [[UIApplication sharedApplication] valueForKey:@"statusBarWindow"];
id statusBar = [statusBarWindow valueForKey:@"statusBar"];

SEL setForegroundColor_sel = NSSelectorFromString(@"setForegroundColor:");
if([statusBar respondsToSelector:setForegroundColor_sel]) {
    // iOS 7+
    [statusBar performSelector:setForegroundColor_sel withObject:YourColorHere];
                                                                 ^^^^^^^^^^^^^
}

我知道我正在访问私有API,但我已经在许多项目中使用了这个,苹果已经批准了它。

在提交应用程序时,将此代码发送到Apple的评论部分,并告知您正在使用此代码更改状态栏的颜色。

是的,不要忘记下面的内容。

不需要做一些额外的,只是写这段代码在你的viewController和获得状态栏颜色白色

- (UIStatusBarStyle)preferredStatusBarStyle{return UIStatusBarStyleLightContent;}

你可以在iOS 6和7中使用这个:

#ifdef __IPHONE_7_0
# define STATUS_STYLE UIStatusBarStyleLightContent
#else
# define STATUS_STYLE UIStatusBarStyleBlackTranslucent
#endif

[[UIApplication sharedApplication] setStatusBarStyle:STATUS_STYLE animated:YES];

这个解决方案适用于使用新的SwiftUI Lifecycle / iOS 14.0的应用程序:

我需要动态更改状态栏文本颜色,无法访问窗口。rootViewController,因为SwiftUI生命周期中不存在SceneDelegate。

我终于找到了Xavier Donnellon的简单解决方案:https://github.com/xavierdonnellon/swiftui-statusbarstyle

复制StatusBarController.swift文件到你的项目中,并将你的主视图包装成一个RootView:

@main
struct ProjectApp: App {     
    var body: some Scene {
        WindowGroup {
            //wrap main view in RootView
            RootView {
                //Put the view you want your app to present here
                ContentView()
                    //add necessary environment objects here 
            }
        }
    }
}

然后你可以通过使用。statusbarstyle (. darkcontent)或。statusbarstyle (. lightcontent)视图修饰符来改变状态栏文本的颜色,或者直接调用例如UIApplication.setStatusBarStyle(. lightcontent)。

不要忘记在Info.plist中设置“基于视图控制器的状态栏外观”为“YES”。