Changing the Color of the Status Bar [duplicate]

2020-02-09 10:22发布

I am trying to change the color of the status bar to like a blue, or some other color.

Is this possible, or does Apple not allow it?

5条回答
forever°为你锁心
2楼-- · 2020-02-09 11:01

I made this extension to change color of status bar

public extension UIViewController {
    func setStatusBar(color: UIColor) {
        let tag = 12321
        if let taggedView = self.view.viewWithTag(tag){
            taggedView.removeFromSuperview()
        }
        let overView = UIView()
        overView.frame = UIApplication.shared.statusBarFrame
        overView.backgroundColor = color
        overView.tag = tag
        self.view.addSubview(overView)
    }
}

Here is usage anywhere in viewcontroller:

setStatusBar(color: .red)

查看更多
姐就是有狂的资本
3楼-- · 2020-02-09 11:15

NOTE: This solution fails under iOS 13 and later.

First in Plist set View controller-based status bar appearance to 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
}

The output screenshot is below

enter image description here

查看更多
萌系小妹纸
4楼-- · 2020-02-09 11:19

No, it's not possible with ready-made public APIs.

But with the release of iOS 7, you’re allowed to change the appearance of the status bar. Hence I am posting my workaround.

From an individual view controller by overriding the preferredStatusBarStyle:

-(UIStatusBarStyle)preferredStatusBarStyle 
{ 
    return UIStatusBarStyleLightContent; 
}

Alternatively, you can set the status bar style by using the UIApplication statusBarStyle method. To do this, insert a new key named “View controller-based status bar appearance” and set the value to NO. enter image description here

By disabling the “View controller-based status bar appearance”, you can set the status bar style by using the following code.

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

At the end, change the UINavigationBar property tint color like below

[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];
查看更多
对你真心纯属浪费
5楼-- · 2020-02-09 11:22

Here is my workaround: create a UIView, add it to your root view of view controller as artificial status bar background
1.Create a UIView

// status bar's height is 20.0pt
CGRect frame = CGRectMake(0.0, 0.0, [UIScreen mainScreen].bounds.size.width, 20.0);
UIView *fakeStatusBarBG = [[UIView alloc] initWithFrame:frame];
fakeStatusBarBG.backgroundColor = [UIColor yourColor];

2.Add it to your view controller's root view

// self is your view controller, make sure fakeStatusBarBG is the top subview in your view hierarchy
[self.view insertSubview:fakeStatusBarBG aboveSubview:yourTopSubview];

There your go.

3.(Additional)Change the content color on status bar, only white or black.

- (UIStatusBarStyle)preferredStatusBarStyle
{
    if (youWantWhiteColor)
    {
        return UIStatusBarStyleLightContent;
    }
    return UIStatusBarStyleDefault;
}

This workaround does not use private API, so you're safe. :-)

查看更多
ゆ 、 Hurt°
6楼-- · 2020-02-09 11:25


You can set background color for status bar during application launch or during viewDidLoad of your view controller.

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:

enter image description here

Here is Apple Guidelines/Instruction about status bar change. 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.

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.
  2. In the viewDidLoad add function - setNeedsStatusBarAppearanceUpdate

  3. override preferredStatusBarStyle in your view controller.

-

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

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

Set value of .plist according to status bar style setup level. enter image description here

查看更多
登录 后发表回答