63

Earlier, I was using iOS 6.1 for my project. Recently I have switched to iOS 7. For, a lot of changes I knew, I updated my code.. But I have observed a strange behavior. My view on every screen gets hidden below navigation bar. Repositioning view solves the problem for iOS7, but creates problems for older iOS versions.

Can anyone explain me, what is the reason and why does it happen?? What has been changed in iOS 7 that's causing this problem??

Any help would be appreciated..

Salman Zaidi
  • 9,342
  • 12
  • 44
  • 61
  • 2
    Check out some of the iOS7 WWDC talks on transitioning your iOS6 apps to iOS7. As the answer below states, you can change your plist. The reason why this is happening is because iOS7 is aiming for edge to edge content, with a feel of 'layers' and 'depth', so now the status bar and navigation bar sits on top of your content, rather than above it. – Tim Sep 16 '13 at 09:57

9 Answers9

131

Try navigationBar.translucent = NO;, It is YES by default in iOS7.

It is also good to take a look on this part of UINavigationBar documentation:

New behavior on iOS 7. Default is YES. You may force an opaque background by setting the property to NO. If the navigation bar has a custom background image, the default is inferred from the alpha values of the image—YES if it has any pixel with alpha < 1.0 If you send setTranslucent:YES to a bar with an opaque custom background image it will apply a system opacity less than 1.0 to the image. If you send setTranslucent:NO to a bar with a translucent custom background image it will provide an opaque background for the image using the bar's barTintColor if defined, or black for UIBarStyleBlack or white for UIBarStyleDefault if barTintColor is nil.

Edit:

Setting 'navigationBar.translucent' value causes exception if you run project in devices/simulators having older iOS versions.

So you can add a version check like this:

float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersion >= 7.0)
{
    navigationBar.translucent = NO;
}

Another option would be to set:

vc.edgesForExtendedLayout = UIRectEdgeNone;

Swift 3:

vc.edgesForExtendedLayout = []
tc333
  • 89
  • 1
  • 7
Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
  • 2
    Thanks for the solution.. It does have solved the problem.. and now I understand as well why it happens :-) – Salman Zaidi Sep 16 '13 at 10:16
  • 2
    if ([self.navigationBar respondsToSelector:@selector(setTranslucent:)]) does not work.. results in crash. I had tried this first.. – Salman Zaidi Sep 17 '13 at 04:05
  • 1
    This is a life saver for those who wants to init the view controllers programmatically – addlistener Sep 02 '14 at 03:28
  • 1
    I think the `edgesForExtendedLayout` solution is probably better, at least in my case, due to the fact that it keeps the nav bar translucent yet the view is positioned correctly. – Greeso Nov 29 '14 at 21:51
  • 1
    self.edgesForExtendedLayout = UIRectEdgeNone; worked for me.. thanks a lot.. +1 – Ahsan Ebrahim Khatri Jun 23 '15 at 10:35
47

You can stop your views going under the navigation bar, in your viewController:

self.edgesForExtendedLayout = UIRectEdgeNone;
Daniel Broad
  • 2,512
  • 18
  • 14
  • 2
    after many headaches this is it, this is the best way to go here. Thanks – PakitoV Nov 19 '13 at 16:02
  • 2
    This also will stop a scrollView which has been programmatically added below (not beneath) the navigationbar/statusbar from automatically changing its content offset *as if it were* underneath the bars. – SG1 Jul 17 '14 at 01:54
21

Swift 3+:

self.edgesForExtendedLayout = []
David Seek
  • 16,783
  • 19
  • 105
  • 136
11

If you do not need translucent navigation bar in your app you can fix this on iOS7 and iOS6 without code changes.

In storyboard select your navigation controller and then open "Attributes Inspector". Then under "Simulated Metrics" set "Top Bar" to some value but not to "translucent":

Setting Top Bar style

Now your views on iOS6 and iOS7 will have the same positioning as before.

gladimdim
  • 752
  • 6
  • 9
9

Point #7 on this list does the trick. You still have to wrap it in iOS 7-checking code like @null's answer:

float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];
if (systemVersion >= 7.0) {
    viewController.edgesForExtendedLayout = UIRectEdgeNone;
}

The whole article is useful to those transitioning to iOS 7.

Lawrence Kesteloot
  • 4,149
  • 2
  • 31
  • 28
7

Use this property for your VC, in-order to avoid overlap of ur statusbar with your VC Swift :

self.edgesForExtendedLayout = []

Objective C

self.edgesForExtendedLayout = UIRectEdgeNone;
Ash
  • 5,525
  • 1
  • 40
  • 34
3

Look up this key: UIViewControllerBasedStatusBarAppearance.

It's used in your app's info PLIST file and will come up as:

View controller-based status bar appearance

This will allow you to control the status bar's appearance. There's a bunch of API changes for status bars, go have a look in the documentation for new UIViewController methods such as

- (void)prefersStatusBarHidden;

Guy Kogus
  • 7,251
  • 1
  • 27
  • 32
2

In Swift 2.2, use

self.edgesForExtendedLayout = .None
Pang
  • 9,564
  • 146
  • 81
  • 122
matthew
  • 343
  • 2
  • 16
2

For me the best way for transparent Navigation Bar is to change the shadowImage and backgroundImage of the bar.

navigationController?.navigationBar.isTranslucent = true
navigationController?.navigationBar.backgroundColor = nil
navigationController?.navigationBar.setBackgroundImage(UIImage(named: "navBarBackground")?.resizableImage(withCapInsets: .zero, resizingMode: .stretch), for: .default)  
navigationController?.navigationBar.shadowImage = UIImage()
saltwat5r
  • 1,107
  • 13
  • 21