Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

ios - Set the status bar to black colour

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 ?

enter image description here

My approach so far :

I added the following in the info.plist

View controller-based status bar appearance --> NO

and in the viewController

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For Xcode 8 / iOS 10 / Swift 3:

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)

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

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.

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...