更改状态栏的颜色

Changing the Color of the Status Bar

我正在尝试将状态栏的颜色更改为喜欢蓝色或其他颜色。

这可能吗,还是 Apple 不允许?

注意:此解决方案在 iOS 13 及更高版本下失败。

首先在 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
}

输出截图如下

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

但是随着 iOS 7 的发布,您可以更改状态栏的外观。因此,我发布了我的解决方法。

从单个视图控制器覆盖 preferredStatusBarStyle:

-(UIStatusBarStyle)preferredStatusBarStyle 
{ 
    return UIStatusBarStyleLightContent; 
}

或者,您可以使用 UIApplication statusBarStyle 方法设置状态栏样式。为此,插入一个名为“View controller-based status bar appearance”的新键并将值设置为 NO。

通过禁用“基于视图控制器的状态栏外观”,您可以使用以下代码设置状态栏样式。

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

最后,改变 UINavigationBar 属性 色调,如下所示

[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];


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

}



这是结果:

这里是关于状态栏变化的Apple Guidelines/Instruction。状态栏中只允许 Dark & light (while & black)。

这是-如何更改状态栏样式:

如果你想设置状态栏样式,应用级别然后在你的`.plist'文件中设置UIViewControllerBasedStatusBarAppearanceNO

如果您想在视图控制器级别设置状态栏样式,请按照以下步骤操作:

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

  3. 在您的视图控制器中覆盖 preferredStatusBarStyle。

-

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

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

根据状态栏样式设置级别设置.plist的值。

这是我的解决方法:创建一个 UIView,将其作为人造状态栏背景添加到视图控制器的根视图中
1.Create一个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 它到你的视图控制器的根视图

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

开始吧。

3.(附加)更改状态栏上的内容颜色,只有白色或黑色。

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

此解决方法不使用私有 API,因此您是安全的。 :-)

我做了这个扩展来改变状态栏的颜色

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)
    }
}

这里是 viewcontroller 中任何地方的用法:

setStatusBar(color: .red)