我对状态栏的行为有点困惑
我不需要透明状态栏我需要它固定和黑色。和iOS6一样
我尝试了很多,我得到了它,它仅在我第一次启动应用程序时显示黑色,当我将设备旋转到横向并再次变为纵向时,它采用导航栏颜色。
我做错了什么。
任何人都可以请指导我。
这就是我得到的
这就是我想要的
我对状态栏的行为有点困惑
我不需要透明状态栏我需要它固定和黑色。和iOS6一样
我尝试了很多,我得到了它,它仅在我第一次启动应用程序时显示黑色,当我将设备旋转到横向并再次变为纵向时,它采用导航栏颜色。
我做错了什么。
任何人都可以请指导我。
这就是我得到的
这就是我想要的
你 cad 做了一个 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];
}
}
}
}
这是适用于 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 仍然存在。没有代表告诉我预览已被点击。如果您有任何想法,请将其留在评论中。