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

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 21:52:48

问题


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:

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.

Options:

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

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.

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

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

C. Programatically add constraints.

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.

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.

E. Mess with iOS6/7 deltas in IB.

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

F. Use a nested UINavigationController.

This is the workaround I selected. See answer below.


回答1:


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.

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.



回答2:


You need to add a Vertical Constraint from your top most view to Top Layout Guide as described in the following article by Apple.

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




回答3:


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



回答4:


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.



来源:https://stackoverflow.com/questions/18912290/how-to-present-a-view-controller-on-ios7-without-the-status-bar-overlapping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!