如何在没有状态栏重叠的iOS7上呈现视图控制器 [英] how to present a view controller on iOS7 without the status bar overlapping

查看:132
本文介绍了如何在没有状态栏重叠的iOS7上呈现视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到当我将我的应用程序迁移到iOS 7时,当呈现视图控制器时,导航栏出现在状态栏下。我想很多人都遇到过同样的问题。这是我所看到的截图:

I'm seeing when I migrated my app to iOS 7, the nav bar is appearing under the status bar when presenting a view controller. I think a lot of people have run into this same issue. Here's a screenshot of what I'm seeing:


  • 必须出现新视图modally,即我需要presentViewController。

  • The new view must appear "modally", i.e. I need presentViewController.

显示某种导航栏或工具栏,状态栏采用导航栏的背景颜色ala iOS 7风格。

Display some sort of nav bar or toolbar, with the status bar taking on the background color of the nav bar ala iOS 7 style.

它必须在iOS 6上运行。

It must work on iOS 6.

我是使用xib处理布局,启用自动布局。

I'm using a xib to handle layout, with autolayout enabled.

呃,我们是否已经回到iOS 5之前的版本并且使用了框架?此外,与自动布局混合通常也不是一个好主意。

Ugh, are we back to the pre-iOS 5 days and mucking with frames? Also it's generally not a good idea mixing with autolayout.

选项A和B的一个缺点是状态栏不会混合到导航栏中:

One disadvantage of options A and B is the status bar won't blend into your nav:

主要缺点是您必须处理约束并计算导航和状态栏高度。哎呀。

The main disadvantage is you'll have to muck with constraints and calculating the nav and status bar heights. Yuck.

在iOS 7上看起来不错,但在iOS 6上有所不同。您需要以编程方式更新导航栏的高度,还要确保视图的其余部分正确更新。凌乱。

Looks good on iOS 7, but breaks on iOS 6. You'll need to programatically update the height of the nav bar, and also make sure the rest of your view updates appropriately. Messy.

多个缺点:您将对ios6 / 7增量进行硬编码。也不适用于autolayout。

Multiple disadvantages: You'll be hardcoding the ios6/7 deltas. Also doesn't work with autolayout.

这是我选择的解决方法。请参阅下面的答案。

This is the workaround I selected. See answer below.

推荐答案

我发现最简单的解决方法是将您想要显示的视图控制器包装在导航控制器中,然后呈现该导航控制器。

The easiest workaround I've found is to wrap the view controller you want to present inside a navigation controller, and then present that navigation controller.

MyViewController *vc = [MyViewController new];
UINavigationController *nav = [[UINavigationController alloc] 
    initWithRootViewController:vc];
[self presentViewController:nav animated:YES completion:NULL];



优点:




  • 不需要框架。

  • 相同的代码适用于iOS 6和iOS 7.

  • 比其他解决方法更难看。

  • Advantages:

    • No mucking with frames needed.
    • Same code works on iOS 6 an iOS 7.
    • Less ugly than the other workarounds.

      • 你可能想要将XIB留空导航栏或工具栏,并以编程方式将UIBarButtonItems添加到导航栏。幸运的是,这很简单。

      这篇关于如何在没有状态栏重叠的iOS7上呈现视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆