【问题标题】:iOS 6 Status Bar to iOS 7iOS 6 状态栏到 iOS 7
【发布时间】:2013-09-17 15:29:05
【问题描述】:

我不明白如何创建与 iOS 6 到 iOS 7 相同的黑色状态栏,我在 SO 上找到了这个问题:iOS 7 status bar back to iOS 6 default style in iPhone app?

在我的应用中,我只有横向模式,所以我使用这个:

info.plist 中将UIViewControllerBasedStatusBarAppearance 设置为NO(选择不让视图控制器调整状态栏样式,以便我们可以使用 UIApplicationstatusBarStyle 方法设置状态栏样式。)

在AppDelegate的应用:didFinishLaunchingWithOptions,调用

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {

 [application setStatusBarStyle:UIStatusBarStyleLightContent];

 self.window.clipsToBounds =YES;

 self.window.frame =  CGRectMake(0,20,self.window.frame.size.width,self.window.frame.size.height-20);
}

return YES;

但这是结果:

有人可以帮忙吗?或建议我另一种解决方案?

【问题讨论】:

  • 看起来,您正在使用导航栏。如果是,则状态栏样式取决于导航栏。请让我知道您的想法,以便我为您提供解决方案。
  • 是的,我想使用导航栏,我还想使用 uitabbar 来控制一些带有导航栏的视图(如上图)和一些没有导航栏的视图......我该怎么做?
  • 请参考下面我的回答。

标签: ios6 statusbar ios7


【解决方案1】:

在某些情况下,导航栏或搜索栏的背景图片可以在状态栏后面向上延伸(详情请参阅下表图片)。如果状态栏下方没有栏,则内容视图应使用屏幕的全高。

根据苹果文档,导航背景图像高度应该正好是 44 磅。在您的情况下,您没有使用任何自定义背景图像,但是您需要默认的 iOS6 状态栏样式。您可以使用以下代码实现此目的,

CGRect rect = CGRectMake(0, 0, 1, 44);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context,[[UIColor colorWithRed:(239/255.0) green:(238/255.0) blue:(239/255.0) alpha:1.0] CGColor]);//Please change RGB value according to ur needs.
CGContextFillRect(context, rect);
UIImage *navigationBackgroundImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[[[self navigationController] navigationBar] setBackgroundImage:navigationBackgroundImage forBarMetrics:UIBarMetricsDefault]; 

现在在 info.plist 和 n AppDelegate 的 application:didFinishLaunchingWithOptions 中将 UIViewControllerBasedStatusBarAppearance 设置为 NO,调用

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {
 [application setStatusBarStyle:UIStatusBarStyleLightContent];//Dark status bar background with white color text
}

希望这有助于解决您的问题。更多详情请参考apple doc

【讨论】:

  • 除非我弄错了,否则他问的是状态栏,而不是导航栏。
  • @JosephDeCarlo,在 iOS7 中,当视图有导航栏时,状态栏依赖于导航栏。 Piero 告诉他他正在使用状态栏和导航栏的组合。