如何在不重叠状态栏的情况下在 iOS7 上呈现视图控制器

编程入门 行业动态 更新时间:2024-10-25 01:34:55
本文介绍了如何在不重叠状态栏的情况下在 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 上呈现视图控制器

本文发布于:2023-11-27 19:42:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639349.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   控制器   状态栏   情况下   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!