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

查看:14
本文介绍了如何在不重叠状态栏的情况下在 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:

  • 新视图必须模态"出现,即我需要presentViewController.

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

显示某种导航栏或工具栏,状态栏采用 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 deltas 进行硬编码.也不适用于自动布局.

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.
  • 比其他解决方法更丑.
    • 您可能希望将 XIB 中的导航栏或工具栏留空,并以编程方式将 UIBarButtonItems 添加到导航栏.幸运的是,这很容易.

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

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