【问题标题】:Setting custom color of status bar text设置状态栏文本的自定义颜色
【发布时间】:2015-09-17 22:32:50
【问题描述】:

在 iOS8 中,我想将状态栏文本(运营商、时间、电池)的颜色设置为自定义颜色。

我已将 View controller-based status bar appearance 设置为 NO 并在单个视图控制器和应用程序委托中尝试了此代码:

[[UINavigationBar appearance] setTintColor:color_font];

[self.navigationController.navigationBar setBarTintColor:[UIColor greenColor]];

【问题讨论】:

  • 您是否将这些指令放在您的 AppDelegate 文件中?
  • 在视图控制器中
  • 将它们添加到您的 AppDelegate 文件中

标签: ios


【解决方案1】:

在 iOS8 中,我想将状态栏的颜色(运营商、时间、电池)设置为自定义颜色。

如果不使用私有 API (Customize iOS 7 status bar text Color),现在这是不可能的。您只能将状态栏的文本设为白色 (UIStatusBarStyleLightContent) 或黑色 (UIStatusBarStyleDefault)。检查文档:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/#//apple_ref/c/tdef/UIStatusBarStyle

您可以在状态栏后面放置一个视图(根据其他人的回答),但我认为这不是您要问的。

【讨论】:

    【解决方案2】:

    在您的 AppDelegate 中尝试这样的操作

    UIView *addStatusBar = [[UIView alloc] init];
    addStatusBar.frame = CGRectMake(0, 0, screenWidth, 20);
    addStatusBar.backgroundColor = [UIColor grayColor]]; //assign here your color 
    [self.window.rootViewController.view addSubview:addStatusBar];
    

    【讨论】:

    • 与其硬编码大小,不如参考状态栏属性。
    • 我对此没有异议。我正在评论您为viewframe 提供的静态大小。
    【解决方案3】:

    转到您的应用 info.plist

    1) 将基于视图控制器的状态栏外观设置为 NO

    然后在你的项目AppDelegate的状态栏上添加Cotom View

    UIView *statusBarViewObj = [[UIView alloc] init];
    statusBarViewObj.frame = CGRectMake(0, 0, screenWidth, 20);
    statusBarViewObj.backgroundColor = [UIColor orangeColor]];  
    [self.window.rootViewController.view addSubview:statusBarViewObj];
    

    【讨论】:

    • 与其硬编码大小,不如参考状态栏属性
    【解决方案4】:

    创建一个视图,将其放在状态栏所在的位置,并将其背景颜色设置为您需要的任何颜色。例如:

    目标 C:

    UIView *statusBarView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,
                            [UIApplication sharedApplication].statusBarFrame.size.width,
                            [UIApplication sharedApplication].statusBarFrame.size.height)];
    statusBarView.backgroundColor  =  [UIColor greenColor]; // Replace with color you desire
    [self.view addSubview:statusBarView];
    

    斯威夫特:

    let statusBarView = UIView(frame: CGRectMake(0, 0,
                        UIApplication.sharedApplication().statusBarFrame.size.width,
                        UIApplication.sharedApplication().statusBarFrame.size.height))
    statusBarView.backgroundColor = UIColor.greenColor() // Replace with color you desire
    self.view.addSubview(statusBarView)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多