Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationController has extra status bar gap at top

This looked simple enough when I set it up, but I can't explain why this gap is present between the status bar and the navigation bar. Also, the contained view looks like it may be properly aligned, and it's just the nav bar that is shifted down. The gap looks like the size of the status bar, so I expect that has something to do with it, but I don't know what.

Nav Bar with extra space

Here is the code for setting up the navigation controller:

- (void)viewDidLoad
{
    [super viewDidLoad];

    advancedVC = [[AdvancedSearchFormVC alloc] initWithNibName:@"AdvancedSearchForm" bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:advancedVC];
    nav.navigationBar.tintColor = [UIColor defaultNavBarTint];
    nav.navigationBar.topItem.title = NSLocalizedString(@"SearchTitle", nil);
    UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"SearchButton", nil) style:UIBarButtonItemStylePlain target:self action:@selector(refreshPropertyList:)];          
    nav.navigationBar.topItem.rightBarButtonItem = searchButton;

    self.view = nav.view;
}

The rootViewController uses a view from a xib file, where I have simulated the status bar, the navigation bar, and the tab bar.

like image 327
Jim Avatar asked Jan 15 '12 21:01

Jim


3 Answers

The problem is indeed that the navigation controller always expects to leave room for the status bar, which is the 20 pixel gap. I searched around for a while before I found this solution which works:

//nav is assumed to be a subclass or instance of UINavigationController
nav.view.frame = CGRectOffset(nav.view.frame, 0.0, -20.0);
//you can then add the navigation's view as a subview to something else

I originally found an answer which did this offset to the navigationbar's view, but it didn't work. It works when you do it to the navigation controllers actual view.

I use this technique to add a navigation controller from another nib to an empty view of my main nib, so I can position it anywhere within the main screen as a subview. By using an empty view as a placeholder and positioning frame on my main nib, I create a separate nib and class to manage the navigation, which manages other nibs used to handle their screens. This way I can solve the classic "how do I add a banner, image, or custom views above my navigation controller" while having a navigation controller as a subview...in iOS 5 to be specific.

It's also worth mentioning that I use the app delegate to store and access all the other controllers, so they are retained by a persistant instance which I can access from any class. Create and synthesise some properties in the app delegate of all your controllers, and in viewDidLoad create instances. That way I can reference all the controllers used in my app later anywhere by adding:

//this shows how to store your navigation controllers in the app delegate
//assumes you've added 2 properties (UINavigationController*)"navController" and (UIViewController*)"rootController" in your app delegate
//...don't forget to add #import "AppDelegate.h" to the top of the file

AppDelegate *app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[app.navController pushViewController: app.rootController animated:YES];
//now apply the offset trick to remove the status gap
app.navController.view.frame = CGRectOffset(app.navController.view.frame, 0.0, -20.0);
like image 73
Ali Avatar answered Nov 19 '22 15:11

Ali


I had the same problem before. The code I used to add UINavigationBar to UIViewController:

UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:self];
[self.view  addSubview:nc.view];

Solution:
Check the box "Wants Full Screen" with Attributes inspector of your UIViewController.

like image 29
surlac Avatar answered Nov 19 '22 15:11

surlac


You can try to set the attribute Under Top Bars unchecked from Attributes section of UIViewController.

like image 7
aozan88 Avatar answered Nov 19 '22 16:11

aozan88