是否可以为所有视图控制器更改状态栏颜色?

6

我已经搜索了一段时间,但只找到了描述如何在一个视图控制器上更改颜色的答案,并没有针对所有视图控制器的方法。

这是否有可能实现?

7个回答

13

只需要两个步骤就可以改变整个应用程序的状态栏样式。

第一步

在项目的Info.plist文件中添加一个新属性,并将其设置为false

<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

步骤二

前往您的项目目标,在 通用 / 部署信息 下,将 状态栏样式默认更改为浅色

状态栏样式选项位于 Xcode 项目编辑器的设备方向选择器右下方。

执行这些步骤将确保整个项目中状态栏的行为相同。


6

AppDelegate.swift 中设置状态栏的样式:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    UIApplication.shared.statusBarStyle = .lightContent

    return true
}

将以下代码添加到您的 Info.plist 中:

<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>

2
显然,自iOS 9.0以来,这已经被弃用了... - Clément Cardonnel

2

首先在info.plist中将“View controller-based status bar appearance”设置为NO

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.

let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
   if statusBar.responds(to:#selector(setter: UIView.backgroundColor)) {
        statusBar.backgroundColor = UIColor.blue
    }
    UIApplication.shared.statusBarStyle = .lightContent

    return true
}

以下是输出截图: 在此输入图片描述

这不是一个黑客行为吗?应用程序可能因此被拒绝吗? - jayant rawat
是的,我看到了多个回答都在说这个。你也可以看看上面的其中一个答案。 - jayant rawat

2

您可以在应用启动期间或在视图控制器的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
    }

}



这是结果:

enter image description here


这里是关于状态栏变化的苹果指南/说明。只有深色和浅色(白色和黑色)可以在状态栏中使用。

以下是- 如何更改状态栏样式

如果您想要设置应用程序级别的状态栏样式,请在`.plist'文件中将UIViewControllerBasedStatusBarAppearance设置为NO

或者您可以从应用程序委托中以编程方式进行更改:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    application.statusBarStyle = .lightContent
    return true
}

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

  1. 如果你只需要在UIViewController级别上设置状态栏样式,那么请在.plist 文件中将UIViewControllerBasedStatusBarAppearance设置为YES
  2. viewDidLoad函数中添加setNeedsStatusBarAppearanceUpdate函数。

  3. 重写您的视图控制器中的preferredStatusBarStyle函数。

-

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

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

根据状态栏样式设置级别来设置.plist的值。 enter image description here


1
在AppDelegate.swift文件中添加以下扩展:

Swift 4

extension UINavigationController {
    override open var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
}

在我看来,这应该是现今被接受的答案。 - Peter Grundner

0

是的, 步骤1: 打开您的info.plist文件并插入一个名为“View controller-based status bar appearance”的新键,将其设置为NO

步骤2: 打开您想要更改statusBarStyle的viewcontroller文件,并在viewWillAppear()中放入以下代码:

UIApplication.shared.statusBarStyle = .lightContent

步骤三:

此外,为该特定视图控制器实现viewWillDisappear()方法,并添加以下代码行:

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
}

祝一切顺利


0

重要澄清

了解自定义状态栏的两种方法非常重要。

是否可以为所有视图控制器更改状态栏颜色?

布尔答案是,但是,在合法的方式下,如果您需要除黑色或白色以外的颜色,则回答“是”是具有挑衅性的。

当涉及到自定义状态栏外观时,有两种方法。

第一种方法-整个应用程序的单一颜色

在info.plist中,您可以找到或创建一个名为

View controller-based status bar appearance

并将其设置为NO

它是做什么的?它实际上建立了一个设置,该设置表示在您的应用程序中,状态栏外观不是由每个视图控制器单独定义的。这非常重要。这意味着您拥有整个应用程序的统一设置,适用于所有屏幕。这就是您所需要的。但是。您只能使用两个设置:白色和黑色。而且没有办法使用文档化的API进行自定义。

要设置此项:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    application.statusBarStyle = .lightContent
    return true
}

第二种方法 - 为每个视图控制器设置单独的颜色

这相反于第一种方法。要使其起作用,请前往 info.plist 并设置

View controller-based status bar appearance

YES

这样,每当打开新的视图控制器时,如果您在每个需要的 UIViewController 实例中插入此实现,则状态栏风格会被单独设置:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

你可以像第一种方法一样,为状态栏设置深色或浅色风格。

第三种方法 - 黑客!

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    if let statusbar = value(forKey: "statusBar") as? UIView {
        statusbar.backgroundColor = UIColor.blue
    }

    return true
}

为什么要黑客?如果您需要状态栏颜色而不是黑色或白色。在这里,您可以使用未记录的API。您可以使用KVC获取statusBar对象并手动设置其颜色。这是一种肮脏、非法的方式,但到目前为止,这是设置状态栏自定义颜色的唯一方法。这可能会导致您的应用被拒绝。但也许你很幸运。为了一劳永逸地设置它,您需要将上述标志设置为NO,以便状态栏不会在每个视图控制器中初始化其样式。

这个类不符合键值编码“statusBar”的规范。有任何解决方案吗? - Yogesh Patel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接