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

10

100

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.

Kegan answered 23/9, 2013 at 7:7 Comment(1)
Here you are another solution for this issue. https://mcmap.net/q/212560/-static-fixed-navigation-bar-in-iosPareu
L
238

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.

Lesterlesya answered 23/9, 2013 at 7:11 Comment(10)
I have placed this under viewDidLoad however it yields no changes.Kegan
make it in viewDidLayoutSubviews funtionsTimoteo
Appending this to viewDidLayoutSubviews did the trick. I appreciate the help gentlemen!Kegan
Hi, Any Question? or is it compliment?Timoteo
@kokx I have implemented whatever you and said but I am still having same problemRima
try in -(void) viewWillLayoutSubviews methodLesterlesya
For detailed insight read iOS 7 UI Transition Guide. Chapter: Appearance and Behavior. [1]:Birefringence
Worked like a charm for me in AppDelegate didFinishLaunchingWithOptions. THANK you Deepesh. Upvote.Dislike
In xcode5, you can set the Navigation Bar to be not Translucent under the Attributes Inspector.Hayse
self.edgesForExtendedLayout = UIRectEdgeNone; helped mePale
I
34

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;
Iraidairan answered 23/9, 2013 at 7:11 Comment(2)
There is no "navController" property. Use this: self.navigationController.navigationBar.translucent = NO;Stripper
i create navigtionbarcontroller object in app delegate and give name navController.Iraidairan
L
21

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

enter image description here

Liftoff answered 25/6, 2015 at 14:5 Comment(0)
K
12

This works for swift as well on iOS 8.1

navigationController?.navigationBar.translucent = false
Kashmiri answered 13/1, 2015 at 12:22 Comment(0)
M
8

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

Moia answered 12/2, 2014 at 17:34 Comment(3)
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.Forrester
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.Moia
This works but looks like a hack. Is there any notice in official documentation about this?Ardith
N
4

Swift 4:

Set following line of code in viewDidLoad method:

self.navigationController?.navigationBar.isTranslucent = false
Nancynandor answered 4/10, 2017 at 5:31 Comment(0)
C
3

You can add this method into your view controller as shown on this URL:

-(void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    self.searchBar.frame = 
    CGRectMake(0, self.topOfViewOffset, self.searchBar.frame.size.width, self.searchBar.frame.size.height);
}
Cornew answered 22/11, 2013 at 11:29 Comment(0)
M
2

Another approach is to set self.automaticallyAdjustsScrollViewInsets = YES; on your view controller. This is enabled by default. But in your case:

I see you are using EGORefreshHeaderView. It plays with contentInset of UITableView. So when you release it, header will reset top inset instead of restore previous value.

Maureen answered 5/4, 2014 at 19:55 Comment(0)
N
-1

If you want to have complete control on views and avoid faulty adjustments of iOS, subclass UITableView and adjust the insets (both scroll and indicators) in -(void)willMoveToWindow:(UIWindow *)newWindow. Works for me.

Neutralize answered 4/6, 2014 at 21:38 Comment(0)
F
-4

Another option is to open your Info.plist file in source code mode and enter the following info:

<key>UIViewControllerBasedStatusBarAppearance</key>
<false/>
<key>UIStatusBarHidden</key>
<true/>

Hope this helps.

Fundus answered 11/11, 2013 at 17:52 Comment(1)
I don't think this would affect the view at all.Jourdan

© 2022 - 2024 — McMap. All rights reserved.