将状态栏设置为黑色 [英] Set the status bar to black colour

查看:216
本文介绍了将状态栏设置为黑色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将状态栏的颜色(显示时钟和电池的顶部的小条带)设置为带有白色文本的黑色背景。我该怎么做?

I want to set the colour of the status bar (The small strip on top where the clock and battery is displayed) to black background with white text. How can I do this ?

< img src =https://i.stack.imgur.com/qe9Uf.pngalt =在此处输入图像说明>

我的方法到目前为止:

我在 info.plist中添加了以下内容

View controller-based status bar appearance --> NO

viewController

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

但是,样式不会改变。如何让状态栏变黑?

However, the style does not change. How can I make the status bar black?

推荐答案

对于Xcode 8 / iOS 10 / Swift 3:

For Xcode 8 / iOS 10 / Swift 3:

通过在状态栏下添加彩色UIView,在一个视图上临时更改状态栏背景颜色。我将这些行放在 viewWillAppear()中。我的色调是白色的,透明度为45%,但你可以放入任何不透明或透明的UIColor值。

Change the status bar background color temporarily on one view by adding a colored UIView under the status bar. I put these lines in viewWillAppear(). my tint is white with 45% transparency, but you can put in any opaque or transparent UIColor value.

let statusView = UIView(frame: CGRect(x: 0, y: 0, width:     self.view.bounds.width, height: 20))
statusView.backgroundColor = UIColor.white.withAlphaComponent(0.45)
 self.view.addSubview(statusView)

您可以将statusView添加到navigationController,而不是视图,如果你在视图上有一个导航控制器。

You can add your statusView to the navigationController, instead of the view, if you have a navigation controller on the view.

您还可以传入UIScreen.main.bounds.size.width作为模拟状态栏背景视图的宽度。不要硬编码宽度,否则它不适合所有设备。

You can also pass in UIScreen.main.bounds.size.width for the width of the simulated status bar background view. Do not hardcode a width, otherwise it wont fit all devices properly.

或者,您也可以使用此代码更改整个应用中的状态栏背景。请注意,即使放在 viewWillAppear(),而不是 viewDidLoad(),其他视图仍会采用更改向前/向后导航时的状态栏。

Alternatively, you can also change the status bar background throughout your app using this code. be aware that if even if placed in viewWillAppear(), not viewDidLoad(), other views will still adopt the altered status bar when you navigate forward/back.

let statWindow = UIApplication.shared.value(forKey:"statusBarWindow") as! UIView
let statusBar = statWindow.subviews[0] as UIView
statusBar.backgroundColor = UIColor.white.withAlphaComponent(0.45)

这篇关于将状态栏设置为黑色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆