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

时间:2013-09-19 19:08:41

标签: ios objective-c xcode ios6 ios7

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

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

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

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

5 个答案:

答案 0 :(得分:6)

如果您使用的是auto layout,那么您需要做的就是从最顶层的视图添加Vertical ConstraintTop Layout Guide,如下所示,它应该注意顶部间距。

enter image description here

  

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

答案 1 :(得分:5)

您可以使用iOS6 / 7增量的新Xcode 5功能将-20设置为所有视图,这将为您提供类似的体验。在界面构建器中为iOS7正确设置视图,并使用增量来支持iOS6。

答案 2 :(得分:5)

以下是我用20px(状态栏的高度)填充视图顶部所做的工作。

我在AppDelegate的应用程序中使用了这段代码:didFinishLaunchingWithOptions:method

...
// 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中执行此操作一次。

答案 3 :(得分:4)

设置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`方法。)

在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;

答案 4 :(得分:2)

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

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