iOS 7 Navigation Bar Hiding Content

2020-02-17 09:45发布

I have an app that was developed in iOS 6. But now in iOS 7 or even my app compiled for iOS 6, but running on an iOS 7 device the top navigation bar (the new giant one in iOS 7) my content is hidden. The top navigation bar covers it. If I manually move it down with CGRect it looks good in iOS 7, but now iOS 6 looks horrible (to much space above it).

The app was built with autolayout off because autolayout is way to difficult to get things setup correctly.

My question is, is there an easy way to move the content down for iOS 7 only? I really don't want to have to turn autolayout back on and spend a month trying to get all the UI elements back in place. The app is pretty sophisticated with 30+ screens and a lot of animating view on screens.

4条回答
家丑人穷心不美
2楼-- · 2020-02-17 10:07

Put self.edgesForExtendedLayout = UIRectEdgeNone;

in your ViewDidLoad method

查看更多
Luminary・发光体
3楼-- · 2020-02-17 10:17

You can also try setting the navigationBar.translucent = NO, as was mentioned in this answer.

查看更多
我只想做你的唯一
4楼-- · 2020-02-17 10:21

I think there's still a little bit of misconception going around this layout problem even if iOS 7 was rolled out more than a year ago. So I eventually decided to elaborate further my answer.

Here's the thing.

Because automaticallyAdjustsScrollViewInsets' default value is YES, a pretty straightforward solution could be adding the following code:

if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) { // if iOS 7
    self.edgesForExtendedLayout = UIRectEdgeNone; //layout adjustements
}

into the ViewController's -viewDidLoad method.

If you wish to remove the status bar quirk (due to the bar translucency, so it's not weird whatsoever) add self.navigationController.navigationBar.translucent = NO. The default value is YES. Note: this has nothing to do with the content, it's related to the content because of translucency but that's an altogether different story!

Because extendedLayoutIncludesOpaqueBars is NO by default, self.navigationController.navigationBar.translucent = NO means basically having

self.edgesForExtendedLayout = UIRectEdgeLeft | UIRectEdgeRight| UIRectEdgeBottom; 

Or, more generally, something like that (it's like pseudocode to give an idea...)

BOOL enableTopEdge =  extendedLayoutIncludesOpaqueBars && !navigationBarIsTranslucent
self.edgesForExtendedLayout = (enableTopEdge & UIRectEdgeTop) | UIRectEdgeLeft | UIRectEdgeRight | UIRectEdgeBottom; 
查看更多
太酷不给撩
5楼-- · 2020-02-17 10:21

To hide the navigation bar, add the following to your UIViewController:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // Hide the top navigation bar.
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    self.navigationController.navigationBar.translucent = YES;
}

To show the navigation bar, use the following:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // Show the top navigation bar.
    self.navigationController.navigationBar.translucent = NO;
}

Below are the results on iOS7:

Navigation bar is hidden. Navigation bar is shown.

The screenshot on the left is with navigation bar hidden, while the image on the right is with navigation bar displayed - the contents of the table are correctly hidden under the navigation bar.

Hope this helps!

查看更多
登录 后发表回答