【问题标题】:Change the status bar background color color past iOS 7更改 iOS 7 以上的状态栏背景颜色
【发布时间】:2014-01-17 07:27:45
【问题描述】:

我想在 iOS 7 上更改状态栏的背景颜色,我正在使用以下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [UIApplication sharedApplication].statusBarHidden = NO;

        self.window.clipsToBounds = YES;
        [[UIApplication sharedApplication] setStatusBarStyle: UIStatusBarStyleLightContent];
        self.window.frame =  CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
        self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height);

        ...
        ...


}

当我写这篇文章时,它会显示黑色背景的状态栏;我希望它有红色背景。

如何将状态栏的颜色改为红色而不是黑色?

【问题讨论】:

标签: ios cocoa-touch uistatusbar


【解决方案1】:

在iOS 7中处理状态栏背景颜色时,有2种情况

案例 1:使用导航栏查看

在这种情况下,在您的 viewDidLoad 方法中使用以下代码

  UIApplication *app = [UIApplication sharedApplication];
  CGFloat statusBarHeight = app.statusBarFrame.size.height;

  UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -statusBarHeight, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
  statusBarView.backgroundColor = [UIColor yellowColor];
  [self.navigationController.navigationBar addSubview:statusBarView];

案例 2:没有导航栏的视图

在这种情况下,在您的 viewDidLoad 方法中使用以下代码

 UIApplication *app = [UIApplication sharedApplication];
 CGFloat statusBarHeight = app.statusBarFrame.size.height;

 UIView *statusBarView =  [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, statusBarHeight)];
 statusBarView.backgroundColor  =  [UIColor yellowColor];
 [self.view addSubview:statusBarView];

【讨论】:

  • 这非常适合视图控制器未嵌入导航控制器的用例。这是 swift 等价物:let statusBarHeight: CGFloat = UIApplication.shared.statusBarFrame.heightlet overlayFrame: CGRect = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: statusBarHeight)self.statusBarOverlayView = UIView(frame: overlayFrame)self.statusBarOverlayView.barTintColor = UIColor.greenself.view.addSubview(self.statusBarOverlayView)
【解决方案2】:

在 iOS 7 及更高版本中,状态栏是透明。将视图的backgroundColor 设置为状态栏所需的颜色。

或者,您可以在视图顶部添加一个 20 像素高的红色子视图。

请参阅Apple Transition Guide 了解更多信息。

另外,请确保您的 preferredStatusBarStyleUIStatusBarStyleLightContent。并在您的 Info.plist 中将“基于控制器的状态栏外观”设置为“否”。

【讨论】:

  • preferredStatusBarStyle 已经是UIStatusBarStyleLightContent 并且我无法将背景颜色更改为红色,因为我想要带有白色的 bg 并且只有带有红色的状态栏。有没有办法改变status Bar 的颜色?其他然后添加子视图。
  • 不,那么你必须添加框架 CGRectMake(0.0,0.0,320.0,20.0) 的子视图。
  • 将其放入应用委托中的 didFinishLaunchingWithOptions 解决了我的问题。
【解决方案3】:

目标c

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    [[UIApplication sharedApplication] setStatusBarHidden:false];
    UIView *statusBar=[[UIApplication sharedApplication] valueForKey:@"statusBar"];
    statusBar.backgroundColor = [UIColor redColor];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];



    return YES;
}

这对我有用。我希望这对你也有帮助。

【讨论】:

【解决方案4】:

@Teja Kumar Bethina 提供的内容很有帮助,但最好从 UIApplication 单例中获取 statusBar 的高度,如下所示:

UIApplication *app = [UIApplication sharedApplication];

UIView *statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, -app.statusBarFrame.size.height, self.view.bounds.size.width, app.statusBarFrame.size.height)];
    statusBarView.backgroundColor = [UIColor blackColor];

【讨论】:

  • 我同意。必须避免硬编码值。非常感谢!
【解决方案5】:

在 AppDelegate 中使用

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

在项目 info.plist 文件中

设置标志 NO 以在应用中查看基于控制器的状态栏外观。

【讨论】:

  • 它只改变状态栏的文本颜色而不改变背景颜色
  • 它将根据导航栏颜色自动更新。你想和导航栏不一样吗?
【解决方案6】:

从 ViewDidLoad 调用此函数并传递您的状态栏颜色。

func setStatusBarBackgroundColor(color: UIColor)  
{
    guard  let statusBar = UIApplication.sharedApplication().valueForKey("statusBarWindow")?.valueForKey("statusBar") as? UIView 
    else 
    {
    return
    }
    statusBar.backgroundColor = color
}

希望对您有所帮助。

【讨论】:

  • swift 3 语法中的类型转换:let statusBar = (UIApplication.shared.value(forKey: "statusBarWindow") as AnyObject).value(forKey: "statusBar") as? UIView
【解决方案7】:

这是一个使用自动布局的快速解决方案,如果您的应用程序是纵向或横向的,该栏将是正确的宽度。它作为子视图添加到导航栏视图,因此偏移 -20。您可以将其添加到导航栏的父视图中,然后不需要偏移量。此示例还将颜色设置为与导航栏相同。

希望对你有帮助:)

    // Add colored opaque view behind the status bar view
    let statusBarView = UIView()
    statusBarView.backgroundColor = navigationBar.backgroundColor
    statusBarView.translatesAutoresizingMaskIntoConstraints = false
    navigationBar.addSubview(statusBarView)

    let views = ["statusBarView": statusBarView]
    let hConstraint = "H:|-0-[statusBarView]-0-|"
    let vConstraint = "V:|-(-20)-[statusBarView(20)]"

    var allConstraints = NSLayoutConstraint.constraintsWithVisualFormat(hConstraint,
            options: [], metrics: nil, views: views)

    allConstraints += NSLayoutConstraint.constraintsWithVisualFormat(vConstraint,
            options: [], metrics: nil, views: views)

    NSLayoutConstraint.activateConstraints(allConstraints)

【讨论】:

    【解决方案8】:

    如果您的应用程序是基于导航的,那么您可以将状态栏背景颜色设置为红色,如下所示:

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
    

    可能会有所帮助。

    【讨论】:

    • 这段代码对我不起作用 :( ,你可以看到没有UINavigationBar 只有状态栏
    猜你喜欢
    • 1970-01-01
    • 2016-10-16
    • 2013-10-04
    • 2015-09-26
    • 2018-04-10
    • 2019-11-20
    • 2015-06-01
    • 2015-10-09
    • 1970-01-01
    相关资源
    最近更新 更多