hydev
hydev

Reputation: 744

UIViewController not filling screen

I have a UINavigationController that pushes on another UIViewController. In this UIViewController I am going to show a UITableView when in portrait mode and another view in landscape mode.

Therefore in my viewDidLoad I am creating UIView and then adding 2 ViewControllers to this. My problem is that when it loads up I get the following white margin at the top. example

I think this is because of (in my Step 3 below ) the

CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
[[self view] setFrame:appFrame];

is not returning the full screen, minus the navigation bar. Is this right? If so how can I make it return the full size so there is no white margin?

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // Step 1 - Set up the tableDataView for vertical layout
TableDataViewController *tableController = [[TableDataViewController alloc] init];
self.tableDataViewController = tableController;
[tableController release];

// Step 2 - Set up the graphView for horizontal layout
GraphViewController *graphController = [[GraphViewController alloc] 
    initWithNibName:@"GraphViewController" bundle:nil];
self.graphViewController = graphController;
[graphController release];

// Step 3 - Get the screen size
CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
[[self view] setFrame:appFrame];

// Step 4 - Add the vertical view to the containerView
//        and then add the containerView to this controller
containerView = [[[UIView alloc] initWithFrame:appFrame] autorelease]; 
[[self view] addSubview:containerView];                     
[containerView addSubview:[tableDataViewController view]];

// Step 5 - Add to the active view so we can test for it later
activeView = [tableDataViewController view];    
 }

Many thanks

Mike

Upvotes: 1

Views: 4698

Answers (2)

MHC
MHC

Reputation: 6405

There are a couple things going on. Above all, frame describes a view's location in its superview's coordinate system.

containerView seems to be added as a subview of a custom view, and its coordinate system origins below the navigation bar, not the application frame.

You want to fill the custom view, so the frame of containerView should be something like

CGRectMake(0.0, 0.0, self.view.bounds.width, self.view.bounds.height);

Upvotes: 0

user189804
user189804

Reputation:

I think you have an issue with your frame offsets. With the navigation bar enabled the rect you get in appFrame has a y offset of 44.f (the navigation bar's height) - check with NSLog and see if that's true.

Because you are setting the frame of a view that will be placed at the origin it should have x and y origins set to zero. You can do this in a safer manner by checking

CGFloat navHeight = navController.navigationBarHidden ? 0 :
    navController.navigationBar.frame.size.height;

In fact I think using the bounds property of [UIScreen mainScreen] may be a better overall solution. It will come with the origin and size set correctly and you shouldn't need to check the presence of the navigation bar.

Check what's going on:

CGRect screenBounds = [[UIScreen mainScreen] bounds]
NSLog(@"%@", NSStringFromCGRect(screenBounds));
CGRect screenFrame = [[UIScreen mainScreen] applicationFrame]
NSLog(@"%@", NSStringFromCGRect(screenFrame));

Upvotes: 2

Related Questions