Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationController's view and 20 points offset

This is an already posted question but I need to understand what is going on with the following scenario. I'm developing an iPad application and I created a UINavigationController like the following (test purposes).

UINavigationController* navigationController = [[UINavigationController alloc] init];        
navigationController.view.backgroundColor = [UIColor redColor];

Once created, I added the UINavigationController's view as a subview of a UIViewController.

[self.view addSubview:navigationController.view];

The result is displayed in the following image:

enter image description here

As you can see, there is a gap between the status bar and the navigation bar of the UINavigationController. Since the view of the UINavigationController is red, it seems that the frame of the navigation bar has that gap.

I've found an hack to fix the problem. By setting the frame for the UINavigationController, the navigation bar goes in the right position.

CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
CGRect frameRect = CGRectMake(navigationController.view.frame.origin.x, navigationController.view.frame.origin.y - statusBarFrame.size.height, navigationController.view.frame.size.width, navigationController.view.frame.size.height);

navigationController.view.frame = frameRect;

Now, since I don't like very much that hack and I would understand what is going on, do you have any suggestions to find an elegant solution to resolve the problem?

Thank you in advance.

like image 935
Lorenzo B Avatar asked Jun 09 '26 13:06

Lorenzo B


2 Answers

That 20px offset is for status bar, navigation controller is designed to be full-screen, but you are adding it to the subview of the main view.

like image 140
Nickolay Olshevsky Avatar answered Jun 11 '26 11:06

Nickolay Olshevsky


viewController setWantsFullScreenLayout:YES

It may help you.

like image 24
ohmer Avatar answered Jun 11 '26 10:06

ohmer