【问题标题】:black colour for status bar in iOS8iOS8中状态栏的黑色
【发布时间】:2014-11-06 08:58:10
【问题描述】:

我对状态栏的行为有点困惑

我不需要透明状态栏 我需要它固定和黑色。 和iOS6一样

我尝试了很多, 我得到了什么, 只有当我第一次启动应用程序时它才会显示黑色, 当我将设备旋转到横向并再次变为纵向时,它会采用导航栏颜色。

我做错了什么。

谁能指导我。

这就是我得到的

这就是我想要的

【问题讨论】:

  • 你不能那样做。如果你想做同样的事情,那么你必须在同一帧添加视图(0,0,width,20),背景为黑色。并设置View controller-based status bar appearance = NO[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];

标签: ios xcode ios8 ios7-statusbar


【解决方案1】:

你做了一个 hack 来设置状态栏颜色,应用程序不会被 Apple 拒绝,但不能保证 Apple 在下一个 iOS 版本中不会改变这种行为。

- (void)setStatusBarColor:(UIColor *)color {
id statusBarWindow = [[UIApplication sharedApplication] valueForKey:@"_statusBarWindow"];
NSString *statusBarWindowClassString = NSStringFromClass([statusBarWindow class]);

if ([statusBarWindowClassString isEqualToString:@"UIStatusBarWindow"]) {
    NSArray *statusBarWindowSubviews = [statusBarWindow subviews];

    for (UIView *statusBarWindowSubview in statusBarWindowSubviews) {
        NSString *statusBarWindowSubviewClassString = NSStringFromClass([statusBarWindowSubview class]);

        if ([statusBarWindowSubviewClassString isEqualToString:@"UIStatusBar"]) {
            [statusBarWindowSubview setBackgroundColor:color];
        }
    }
}
}

【讨论】:

    【解决方案2】:

    这是适用于 iOS8、Swift、Xcode 6.3 的解决方案。

    添加背景颜色:

    var statusBarBackground = UIView(frame: CGRectMake(0, 0, UIScreen.mainScreen().bounds.width, 30))
    statusBarBackground.backgroundColor = UIColor.blackColor()
    statusBarBackground.tag = 13
    
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    appDelegate.window!.rootViewController!.view.addSubview(statusBarBackground)
    appDelegate.window!.rootViewController!.view.bringSubviewToFront(navigationController!.navigationBar)
    

    去除背景色:

    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    for sv in appDelegate.window!.rootViewController!.view.subviews {
        if sv.tag == 13 {
            sv.removeFromSuperview()
        }
    }
    

    为什么我不直接将 statusBarBackground 添​​加到 viewController 的视图中?因为我需要这个 UIDocumentInteractionController 预览视图,就目前而言,它不允许任何自定义。

    为什么我将 statusBarBackground 设置为 30 高,从而迫使我为 navigationBar 做一个bringSubviewToFront?因为我的导航栏有圆角(有遮罩层,所以没有图像),因此显示了导航栏后面的视图(我希望它与状态栏背景颜色相同。

    还有待弄清楚:如果您点击预览,导航栏会向上移动,但(当然)statusVarBackground 仍然存在。没有代表告诉我预览已被点击。如果您有任何想法,请将其留在 cmets 中。

    【讨论】:

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