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


当前回答

这个答案是在hackingwithswift网站的帮助下得出的

用于iOS (13, *)

有时候我们需要不同颜色的状态栏,例如对于一个ViewController我们需要黑色的状态栏,而对于第二个ViewController我们需要白色的状态栏。 现在我们要做什么? 我们需要在ViewController中添加这个和平代码

    // MARK: - Variables
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    // MARK: - View Life Cycle
    override func viewDidAppear(_ animated: Bool) {
        setNeedsStatusBarAppearanceUpdate()
    }

这段代码将改变特定ViewController中状态栏的浅色或白色。我们可以在preferredStatusBarStyle中将其更改为。dark

欲了解更多细节,请访问hackingwithswift

其他回答

或者,你可以选择退出基于视图控制器的状态栏外观:

在Info.plist中设置“基于视图控制器的状态栏外观”为“NO”。 调用[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

注意:此方法在iOS9中已弃用。在UIViewController上使用preferredStatusBarStyle代替。(见苹果开发者库)

在.plist文件中设置UIViewControllerBasedStatusBarAppearance为YES。 在viewDidLoad做一个[self setNeedsStatusBarAppearanceUpdate]; 增加如下方法: ——(UIStatusBarStyle) preferredStatusBarStyle { 返回UIStatusBarStyleLightContent; }

注意:这并不适用于UINavigationController内部的控制器,请参阅Tyson的评论如下:)

Swift 3 -这将工作在UINavigationController中的控制器。将此代码添加到控制器中。

// Preferred status bar style lightContent to use on dark background.
// Swift 3
override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

Swift 5和SwiftUI

对于SwiftUI,我创建了一个名为HostingController.swift的新swift文件

import Foundation
import UIKit
import SwiftUI

class HostingController: UIHostingController<ContentView> {
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}

然后更改SceneDelegate.swift中的以下代码行

window.rootViewController = UIHostingController(rootView: ContentView())

to

window.rootViewController = HostingController(rootView: ContentView())

以下是苹果关于状态栏更改的指导方针/说明。状态栏只允许显示暗&亮(而&黑)。

这里是-如何改变状态栏样式:

如果你想设置状态栏的风格,应用程序级别,然后设置UIViewControllerBasedStatusBarAppearance为NO在你的。plist文件。

如果你想在视图控制器级别设置状态栏样式,那么按照以下步骤:

如果你只需要在UIViewController级别设置状态栏样式,在.plist文件中将UIViewControllerBasedStatusBarAppearance设置为YES。 在viewDidLoad中添加函数setNeedsStatusBarAppearanceUpdate 重写视图控制器中的preferredStatusBarStyle。

-

override func viewDidLoad() {
    super.viewDidLoad()
    self.setNeedsStatusBarAppearanceUpdate()
}

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

根据状态栏样式设置级别设置.plist的值。

下面是一些在应用程序启动或视图控制器viewDidLoad期间更改/设置状态栏背景色的技巧。

extension UIApplication {

    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }

}

// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
        return true
    }
}


or 
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
    }

}

结果如下:

注:最高赞数的答案不适用于iOS 7 / 8

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

在AppDelegate中添加

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

to

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
   ...
}    

此解决方案适用于iOS 7 / 8。

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

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

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

我希望这能有所帮助。

edit

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