How to prevent UINavigationBar from covering top of view in iOS 7?

97,332

Solution 1

Set the navigation bar's translucent property to NO:

self.navigationController.navigationBar.translucent = NO;

This will fix the view from being framed underneath the navigation bar and status bar.

If you have to show and hide the navigation bar, then use

 if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
    self.edgesForExtendedLayout = UIRectEdgeNone;   // iOS 7 specific

in your viewDidLoad method.

Solution 2

In iOS 7 by defaults all Controller translucent property value is YES, so you set translucent property NO for this issue.

self.navController.navigationBar.translucent = NO;

Solution 3

You can disable the "Extend edges" in Attribute inspector of View Controller of this screen (as shown in below image) :

enter image description here

Solution 4

This works for swift as well on iOS 8.1

navigationController?.navigationBar.translucent = false

Solution 5

If you want to keep the translucency on your navigationBar, at the end of your viewDidLoad or in your viewWillAppear add this line of code:

[self.view sendSubviewToBack:self.tableView]

Somehow if your scrollView subclass (UITableView, UICollectionView, etc.) is at index 0 in your current view subviews, it will automatically adjust the insets according to your navigationBar. And it shouldn't affect your UI in versions prior to iOS7 either.


EDIT If you initialize your UITableView programmatically, then it is best to add it to the view using this [self.view insertSubview:self.tableView atIndex:0];

Share:
97,332
Sam D20
Author by

Sam D20

Updated on July 08, 2022

Comments

  • Sam D20
    Sam D20 almost 2 years

    After updating to Xcode 5, the navigation bars in all of my app's views have shifted down. Here are some screenshots, the first showing everything in the view as it's pulled down, and the second showing all of it untouched. The search bar should begin where the navigation bar.

    All Content All Content on Idle

    Anyone know how I can fix this?

    edit: i have tried this previously recommendation:

    if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
            self.edgesForExtendedLayout = UIRectEdgeNone;
    

    But it yields very odd results.

    Solution Attempt

    This may be because I have a "slide menu" under this view controller that is appearing due to the transparency of the navigation bar.

  • Sam D20
    Sam D20 over 10 years
    I have placed this under viewDidLoad however it yields no changes.
  • Hindu
    Hindu over 10 years
    make it in viewDidLayoutSubviews funtions
  • Sam D20
    Sam D20 over 10 years
    Appending this to viewDidLayoutSubviews did the trick. I appreciate the help gentlemen!
  • Hindu
    Hindu over 10 years
    Hi, Any Question? or is it compliment?
  • Vaibhav Gautam
    Vaibhav Gautam over 10 years
    @kokx I have implemented whatever you and said but I am still having same problem
  • Deepesh
    Deepesh over 10 years
    try in -(void) viewWillLayoutSubviews method
  • Enrico Susatyo
    Enrico Susatyo about 10 years
    I don't think this would affect the view at all.
  • chunkyguy
    chunkyguy about 10 years
  • smitt04
    smitt04 about 10 years
    This should be the correct answer. If you are adding the subview programmatically you can just do [self.view insertSubview:self.tableView atIndex:0]; instead of sending it to the back after you add it.
  • jbouaziz
    jbouaziz about 10 years
    Correct. But without knowing how he's initiating his controller (programmatically, interface builder), we can't assume that it is the best solution. Although I've edited my answer.
  • Cyprus106
    Cyprus106 almost 10 years
    Worked like a charm for me in AppDelegate didFinishLaunchingWithOptions. THANK you Deepesh. Upvote.
  • quik_silv
    quik_silv over 9 years
    In xcode5, you can set the Navigation Bar to be not Translucent under the Attributes Inspector.
  • John Tracid
    John Tracid over 8 years
    This works but looks like a hack. Is there any notice in official documentation about this?
  • Gene Z. Ragan
    Gene Z. Ragan over 8 years
    There is no "navController" property. Use this: self.navigationController.navigationBar.translucent = NO;
  • Sumit Mundra
    Sumit Mundra over 8 years
    i create navigtionbarcontroller object in app delegate and give name navController.
  • luky
    luky over 6 years
    self.edgesForExtendedLayout = UIRectEdgeNone; helped me