简体   繁体   English

状态栏颜色iOS

[英]Status bar color iOS

Somehow, no matter the query or number of answers I come across, I can't find one person who can say how to set the status bar color in iOS using Swift 3. I've seen all the suggestions where you add: 无论如何,无论遇到什么查询或答案,我都找不到一个可以说如何使用Swift 3在iOS中设置状态栏颜色的人。我看到了所有添加的建议:

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

but the thing is, I don't want a transparent status bar. 但问题是,我不需要透明的状态栏。 I want a specific hex color for the status bar color. 我想要状态栏颜色为特定的十六进制颜色。

Here is Apple Guidelines/Instruction about status bar change. 这是有关状态栏更改的Apple准则/说明 Only Dark & light (while & black) are allowed in status bar. 状态栏中仅允许使用深色和浅色(白色和黑色)。

Here is - How to change status bar style: 这是-如何更改状态栏样式:

If you want to set status bar style, application level then set UIViewControllerBasedStatusBarAppearance to NO in your `.plist' file. 如果要设置状态栏样式,请在应用程序级别将UIViewControllerBasedStatusBarAppearance设置为NO

if you wan to set status bar style, at view controller level then follow these steps: 如果要设置状态栏样式,请在视图控制器级别执行以下步骤:

  1. Set the UIViewControllerBasedStatusBarAppearance to YES in the .plist file, if you need to set status bar style at UIViewController level only. 如果仅需要在UIViewController级别设置状态栏样式,则在.plist文件中将UIViewControllerBasedStatusBarAppearance设置为YES
  2. In the viewDidLoad add function - setNeedsStatusBarAppearanceUpdate 在viewDidLoad中添加函数setNeedsStatusBarAppearanceUpdate

  3. override preferredStatusBarStyle in your view controller. 在视图控制器中重写preferredStatusBarStyle。

- --

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

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

Set value of .plist according to status bar style setup level. 根据状态栏样式设置级别设置.plist的值。 在此处输入图片说明


You can set background color for status bar during application launch or during viewDidLoad of your view controller. 您可以在应用程序启动或视图控制器的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
    }

}



Here is result: 结果如下:

在此处输入图片说明

There isn't an API to set the color of the status bar's content; 没有用于设置状态栏内容颜色的API。 if you'd like one, you should file an enhancement request here . 如果您愿意,则应在此处提交增强功能请求。 If you want to change the color of the background behind the status bar, put a view with that background color at the top of your app's window. 如果要更改状态栏后面的背景颜色,请将带有该背景颜色的视图放在应用程序窗口的顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM