具有向后兼容性的iOS 7状态栏半透明

我build立了我的应用程序在iOS 6中有一个半透明的导航栏。我想利用iOS 7中的半透明状态栏,并保持iOS 6中的应用程序,但我的内容总是在iOS 7的状态栏下面20px缺less底部。 我想我可以做非常繁琐的代码更改,检查设备是否具有iOS 7,然后相应地调整我的内容,但是我担心这将会是很多工作。

理想情况下,我想在每个视图控制器视图的顶部添加20像素的填充,以便内容向下移动,并且在iOS 6上使用不透明的导航栏仍然可以正常工作。

我已经阅读了主题上存在的主题1 2 ,但没有提供的答案解决了我的问题。

我应该注意到,我不使用Interface Builder,所有的VC都是以编程方式创build的。

如果您使用的是auto layout ,那么您只需从最顶层视图向Top Layout Guide添加Vertical Constraint ,如下所示,并且应该注意顶部间距。

在这里输入图像说明

欲了解更多信息: https : //developer.apple.com/library/ios/qa/qa1797/_index.html

你可以使用iOS6 / 7增量版的Xcode 5新function,将-20设置为你所有的视图,这会给你类似的体验。 在界面生成器中为iOS7设置正确的视图,并使用增量支持iOS6。

这是我做的总是用20px(状态栏的高度)填充我的视图的顶部。

我在AppDelegate的应用程序中使用了这个代码:didFinishLaunchingWithOptions:方法

 ... // container holds my root view controller UINavigationController *container = [UINavigationController alloc] init...]; ... if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) { // iOS 7 // Create parent controller that will contain your existing root view controller's view shifted 20 px down to account for the status bar. UIViewController *newRootController = [[UIViewController alloc] init]; // Add my old root view controller as a child [newRootController addChildViewController:container]; // Add its view as a subview [newRootController.view addSubview:container.view]; // Call this method because it does some configuration? [container didMoveToParentViewController:newRootController]; // Now just position the view starting at point (0, 20) UIView *aView = container.view; NSDictionary *viewDictionary = NSDictionaryOfVariableBindings(aView); NSArray *constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[aView]|" options:0 metrics:nil views:viewDictionary]; [newRootController.view addConstraints:constraints]; constraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[aView]|" options:0 metrics:nil views:viewDictionary]; [newRootController.view addConstraints:constraints]; self.window.rootViewController = newRootController; } else { // pre iOS 7 self.window.rootViewController = container; } 

现在,只要你在iOS 7中,一切都将存在于视图控制器的视图中,向下移动20个像素。 你只需要在你的AppDelegate中做一次。

UIViewControllerBasedStatusBarAppearance' to NO in info.plist (To opt out of having view controllers adjust the status bar style so that we can set the status bar style by using the设置UIViewControllerBasedStatusBarAppearance' to NO in info.plist (To opt out of having view controllers adjust the status bar style so that we can set the status bar style by using the UIApplicationstatusBarStyle`方法UIViewControllerBasedStatusBarAppearance' to NO in info.plist (To opt out of having view controllers adjust the status bar style so that we can set the status bar style by using the 。)

在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); //Added on 19th Sep 2013 self.window.bounds = CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height); } return YES; 

您可以通过设置以下内容来禁用ios 7中顶部栏下的视图:

 if([controller canPerformAction:@selector(setEdgesForExtendedLayout:) withSender:self]) { [controller setEdgesForExtendedLayout:UIRectEdgeBottom | UIRectEdgeLeft | UIRectEdgeRight]; }