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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18912290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:26:30  来源:igfitidea点击:

how to present a view controller on iOS7 without the status bar overlapping

iosobjective-cuiviewcontrollerios7statusbar

提问by bobics

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:

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

existing nav

existing nav

Requirements:

要求:

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

  • 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.

  • It must work on iOS 6.

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

  • 新视图必须出现“模态”,即我需要presentViewController。

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

  • 它必须在 iOS 6 上运行。

  • 我正在使用 xib 来处理布局,并启用了自动布局。

Options:

选项:

A. Shift your view's frame down by a bit.

A. 将视图的框架向下移动一点。

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.

呃,我们是不是回到了 iOS 之前的 5 天并用框架搞砸了?此外,与自动布局混合通常不是一个好主意。

B. Add a little gap up top below your nav bar.

B. 在导航栏下方的顶部添加一个小间隙。

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

选项 A 和 B 的一个缺点是状态栏不会融入您的导航:

nav with gap

nav with gap

C. Programatically add constraints.

C. 以编程方式添加约束。

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

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

D. Stretch the navigation bar / toolbar's height to include the area of the status bar.

D. 拉伸导航栏/工具栏的高度以包括状态栏的区域。

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.

在 iOS 7 上看起来不错,但在 iOS 6 上会中断。您需要以编程方式更新导航栏的高度,并确保视图的其余部分适当更新。乱。

enter image description here

enter image description here

E. Mess with iOS6/7 deltas in IB.

E. 与 IB 中的 iOS6/7 deltas 混淆。

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

多个缺点:您将对 ios6/7 deltas 进行硬编码。也不适用于自动布局。

F. Use a nested UINavigationController.

F. 使用嵌套的 UINavigationController。

This is the workaround I selected. See answer below.

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

uinavcontroller workaround

uinavcontroller workaround

回答by bobics

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

Advantages:

好处:

  • No mucking with frames needed.
  • Same code works on iOS 6 an iOS 7.
  • Less ugly than the other workarounds.
  • 无需使用框架进行处理。
  • 相同的代码适用于 iOS 6 和 iOS 7。
  • 比其他解决方法丑陋。

Disadvantages:

缺点:

  • You'll probably want to leave your XIB empty of navigation bars or toolbars, and programatically add UIBarButtonItems to the navigation bar. Fortunately this is pretty easy.
  • 您可能希望将 XIB 中的导航栏或工具栏留空,并以编程方式将 UIBarButtonItems 添加到导航栏。幸运的是,这很容易。

回答by Yas T.

You need to add a Vertical Constraintfrom your top most view to Top Layout Guideas described in the following article by Apple.

您需要将垂直约束从最顶部视图添加到顶部布局指南,如 Apple 的以下文章中所述。

https://developer.apple.com/library/ios/qa/qa1797/_index.html

https://developer.apple.com/library/ios/qa/qa1797/_index.html

enter image description here

enter image description here

回答by Jwlyan

Next code worked for me. Just put it to the controller which is presenting the new controller.

下一个代码对我有用。只需将它放在呈现新控制器的控制器上。

#pragma mark hidden status bar
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
}

回答by thorb65

open you xib file and select the viewcontroller. in the inspector tab select the attributes and select in TopBar "Opaque Navigation Bar". this solved the problem for me.

open you xib file and select the viewcontroller. in the inspector tab select the attributes and select in TopBar "Opaque Navigation Bar". this solved the problem for me.