【问题标题】:iOS 7 status bar translucency with backwards compatibility具有向后兼容性的 iOS 7 状态栏半透明
【发布时间】:2013-09-25 00:38:39
【问题描述】:

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

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

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

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

【问题讨论】:

标签: ios objective-c xcode ios6 ios7


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • xCode 在哪里可以提到 iOS6 的增量?
    • 我不得不提到所有子视图的 delta-x 和 delta-height。然后它对我有用。
    • 您可以commant+a 所有视图并在增量中设置相同的值。
    • 他说他没有使用 Interface Builder 来创建他的视图,所以我不确定这是否适合他。
    【解决方案3】:

    这就是我总是用 20 像素(状态栏的高度)填充视图顶部的方法。

    我在我的 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 中执行一次。

    【讨论】:

    • 我试过了,但收到异常“无法同时满足约束”。
    【解决方案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 theUIApplicationstatusBarStyle`方法。)

    在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 6 构建时对我不起作用。此外,找不到状态栏样式。
    • 这不适用于 iOS 6。
    【解决方案5】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-08-14
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 2014-01-21
      • 1970-01-01
      • 2011-02-22
      • 1970-01-01
      • 2019-06-27
      相关资源
      最近更新 更多