Lorenzo B
Lorenzo B

Reputation: 33428

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.

Upvotes: 3

Views: 7435

Answers (4)

artur-dev
artur-dev

Reputation: 350

[self setWantsFullScreenLayout:<#(BOOL)#>];

is deprecated. If you want to add subviews to your ViewController without worrying about NevigationBar use:

self.edgesForExtendedLayout = UIRectEdgeNone;

Upvotes: 0

Swaroop S
Swaroop S

Reputation: 520

This is the fix that really solved all the problem. So thought of posting it as it might really.

How this works

This gonna set my navigation bar origin to 0, and in turn the view of navigation is also set to 0 which takes the reference of Navigation Bar, which solves all the mess :)

And 44 is the heigh of Navigation Bar in the code. :)

Put the code in ViewDidAppear,

   CGRect windowFrame = [UIScreen mainScreen].applicationFrame;
   //Setting the Origin to 0 of both Navigation Bar and Navigation View for Both Landscape and Portrait
   if (UIDeviceOrientationIsPortrait([UIDevice currentDevice].orientation)){
       [self.navigationController.navigationBar setFrame:CGRectMake(0, 0, windowFrame.size.width, 44)];
        [self.navigationController.view setFrame:CGRectMake(0, 0, windowFrame.size.width, windowFrame.size.height)];
    }
    else{
        [self.navigationController.navigationBar setFrame:CGRectMake(0, 0, windowFrame.size.height, 44)];
        [self.navigationController.view setFrame:CGRectMake(0, 0, windowFrame.size.height, windowFrame.size.width)];
    }

Upvotes: 1

ohmer
ohmer

Reputation: 51

viewController setWantsFullScreenLayout:YES

It may help you.

Upvotes: 1

Nickolay Olshevsky
Nickolay Olshevsky

Reputation: 14160

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.

Upvotes: 4

Related Questions