改变状态栏的颜色。

13

我想把状态栏的颜色改成蓝色或者其他颜色。

这个能做到吗?还是苹果不允许这样做?


1
根据两个答案(带有评论),答案(就像我想的那样)是不行。你可以(1)更改状态栏的字体颜色或(2)使用私有API,但这样做会冒苹果拒绝你的应用程序的风险(或更糟糕的是,在某个时候拒绝它)。 - user7014451
5个回答

16

注意:此解决方案在 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
}

The output screenshot is below

enter image description here


6
使用私有且未公开的API可能无法通过应用审核,这样做从来都不是一个好主意。特别是当私有API在未来的iOS更新中发生变化时,很可能会导致崩溃,尤其是按照目前的写法。 - rmaddy
我有一个应用因为这个被拒绝了,而另一个应用很顺利地被接受了。他们确实认为这是私有API的使用,所以在审核过程中你需要靠运气:) - @Sebyddd - byJeevan
我肯定会在你的回答中加入这个事实。 - user7014451
@rmaddy,您会很高兴知道,这段代码最初是为iOS 10编写的,并且自那时以来一直在愉快地工作,但在iOS 13上运行时却导致我的应用程序崩溃。 - Jamie Birch
1
@JamieBirch 这并不让我感到高兴,但它确实验证了挖掘私有API的坏主意。 - rmaddy

9

不,使用现成的公共API是不可能的。

但是随着iOS 7的发布,你可以改变状态栏的外观。因此,我在这里发布我的解决方法。

通过覆盖preferredStatusBarStyle,可以从单个视图控制器中更改状态栏的外观:

-(UIStatusBarStyle)preferredStatusBarStyle 
{ 
    return UIStatusBarStyleLightContent; 
}

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

enter image description here

通过禁用“View controller-based status bar appearance”,您可以使用以下代码设置状态栏样式。

[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

最后,像下面这样更改UINavigationBar属性的色调颜色。
[[UINavigationBar appearance] setBarTintColor:[UIColor blueColor]];

为了澄清你的回答,这个改变状态栏背景颜色为特定颜色吗? - user7014451
请参考此链接:http://www.appcoda.com/customize-navigation-status-bar-ios-7/ - byJeevan
抱歉,但我没有看到OP所要求的内容。更改状态栏的样式会更改字体,而不是背景颜色。我发现与状态栏相关的唯一其他事情就是隐藏它。(我知道这篇文章是几年前的了。它仍然是一篇好文章。但它并没有回答设置状态栏背景颜色的具体问题,我认为这不能通过公共API完成。) - user7014451
@dfd 我完全同意你的观点。但是,我的回答提供了一个合法(苹果公司)的解决问题的方式,我认为这是最好的可能解决方案。 :-) - byJeevan
1
它实际上并没有解决问题,仅仅展示了自定义状态栏的限制。对于此特定的OP,正确答案是“不,使用公共API不可能”。 - user7014451
显示剩余2条评论

6


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

}



这里是更改状态栏样式的 苹果指南/说明 ,只允许在状态栏中使用深色和浅色(白色和黑色)。

以下是如何更改状态栏样式:

如果您想要在应用程序级别设置状态栏样式,请在 `.plist' 文件中将 UIViewControllerBasedStatusBarAppearance 设置为 NO

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

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

  3. 重写您的视图控制器中的 preferredStatusBarStyle 方法。

-

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

override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
}

根据状态栏样式设置水平,设置.plist的值。 enter image description here


1
这应该被更新以指明它在iOS 13下会崩溃。不要随意尝试使用私有API。 - rmaddy
@rmaddy - 感谢您的更新。我会检查并更新这个答案。 - Krunal

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

// 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,所以您可以放心使用。 :-)


2

我制作了这个扩展来改变状态栏的颜色。

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

以下是在视图控制器中使用的示例:

setStatusBar(color: .red)

翻译:设置状态栏颜色为红色。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接