How to prevent UINavigationBar from covering top of view in iOS 7?
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) :
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];
Sam D20
Updated on July 08, 2022Comments
-
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.
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.
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 over 10 yearsI have placed this under
viewDidLoad
however it yields no changes. -
Hindu over 10 yearsmake it in viewDidLayoutSubviews funtions
-
Sam D20 over 10 yearsAppending this to viewDidLayoutSubviews did the trick. I appreciate the help gentlemen!
-
Hindu over 10 yearsHi, Any Question? or is it compliment?
-
Vaibhav Gautam over 10 years@kokx I have implemented whatever you and said but I am still having same problem
-
Deepesh over 10 yearstry in -(void) viewWillLayoutSubviews method
-
Enrico Susatyo about 10 yearsI don't think this would affect the view at all.
-
chunkyguy about 10 yearsFor detailed insight read iOS 7 UI Transition Guide. Chapter: Appearance and Behavior. [1]:
-
smitt04 about 10 yearsThis 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 about 10 yearsCorrect. 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 almost 10 yearsWorked like a charm for me in AppDelegate didFinishLaunchingWithOptions. THANK you Deepesh. Upvote.
-
quik_silv over 9 yearsIn xcode5, you can set the Navigation Bar to be not Translucent under the Attributes Inspector.
-
John Tracid over 8 yearsThis works but looks like a hack. Is there any notice in official documentation about this?
-
Gene Z. Ragan over 8 yearsThere is no "navController" property. Use this: self.navigationController.navigationBar.translucent = NO;
-
Sumit Mundra over 8 yearsi create navigtionbarcontroller object in app delegate and give name navController.
-
luky over 6 yearsself.edgesForExtendedLayout = UIRectEdgeNone; helped me