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


当前回答

在信息。plist设置“视图基于控制器的状态栏外观”为NO

在AppDelegate中添加

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

其他回答

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

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

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

我希望这能有所帮助。

edit

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

对我来说,什么都没用。我试图改变状态栏颜色在ViewController2,这是嵌入在NavigationController,这反过来,从ViewController1被模态地呈现。这种方法行不通:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .darkContent
}

什么都没有发生,直到我找到了这个解决方案: 将这一行添加到ViewController1

导航控制器。modalPresentationCapturesStatusBarAppearance = 真正的

let navigationController = UINavigationController(rootViewController: viewController2)
navigationController.modalPresentationStyle = .overFullScreen
navigationController.modalTransitionStyle = .crossDissolve           
navigationController.modalPresentationCapturesStatusBarAppearance = true
self.present(navigationController, animated: true)

所以如果你的导航方案类似于ViewController1呈现的ViewController2,尝试modalPresentationCapturesStatusBarAppearance属性呈现的一个

文档:

The default value of this property is false. When you present a view controller by calling the present(_:animated:completion:) method, status bar appearance control is transferred from the presenting to the presented view controller only if the presented controller's modalPresentationStyle value is UIModalPresentationStyle.fullScreen. By setting this property to true, you specify the presented view controller controls status bar appearance, even though presented non-fullscreen. The system ignores this property’s value for a view controller presented fullscreen.

在信息。plist设置“视图基于控制器的状态栏外观”为NO

在AppDelegate中添加

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

非常简单的方法来改变状态栏的颜色。 创建navigation Controller的子类。

在view didload方法中编写以下代码。 在所有视图控制器中执行此代码

self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName :
                                                                        [UIColor whiteColor],
                                               NSFontAttributeName:[UIFont boldSystemFontOfSize:19]};

这个解决方案适用于使用新的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”。