Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone: Weird space at the top of UINavigationController

I'm having a strange problem with adding a UINavigationController to my iPhone application. I add the controller as follows:

myViewController *viewController = [[myViewController alloc] initWithNibName:@"myView" bundle:nil];

myNavigationViewController *navigationController = [[myNavigationViewController alloc] initWithRootViewController:viewController];

UIView *finalView = myeNavigationViewController.view;

[self.view addSubview:finalView];

All seems to work as planned except I get a weird white space at the top of my view between the status bar and the UINavigationController title bar. alt text http://www.andrewskinner.name/problem.png

I've searched online but don't really know what to search for. Has anyone else had this problem? Can you point me in the direction of some help?

Thanks in advance.

like image 339
andrewdotcom Avatar asked Jun 24 '09 21:06

andrewdotcom


3 Answers

What does the line

UIView *finalView = myeNavigationViewController.view;

add to the code? It's redundant as you can add the view directly without assigning it to a UIView first - plus it's incorrect as it references the myNavigationController and not navigationController..
I tend to do this

myViewController *viewController = [[myViewController alloc] initWithNibName:@"myView" bundle:nil];    
myNavigationViewController *navigationController = [[myNavigationViewController alloc] initWithRootViewController:viewController];
[navigationController.view setFrame: [self.view bounds]];
navigationController.delegate = self;
[self.view addSubview:[navigationController view]];

Setting the frame to the bounds also removes the white space at the top you were asking about.

like image 196
Craig Avatar answered Oct 25 '22 14:10

Craig


Check out the answers in this question:

Not sure why UIView is being nudged up by around 10px

like image 32
mac_55 Avatar answered Oct 25 '22 16:10

mac_55


The issue is that UINavigationController ideally should be the direct subView of UIWindow. It will position and size right by itself. When you add UINavigationController into another custom view of a UIWindow subview, you need to take care of the position and size of this custom view by taking into account whether the status bar is shown or not in the UIWindow.

My suggestion is to make the custom view as a subclass of UINavigationController:

mySubClass_NavigationController*nav=[[mySubClass_NavigationController alloc] initWithRootViewController:viewController ];

[myUIWindow addSubview:nav.view];

and inside the mySubClass_NavigationController, you can do all the customization that you are doing now in your self (whatever that controller is).

like image 2
Wayne Lo Avatar answered Oct 25 '22 15:10

Wayne Lo