iPhone + CGAffineTransFormRotate(pi/2) + statusBarHidden:YES + presentModalViewController = 20 pixels of white space

15,568

Solution 1

Ok, I figured it out. As usual it was the result of more than 1 issue. These problems all occur before I even push the modal view:

First, In my code I was resizing my view like so:

[self.view setFrame: [[UIScreen mainScreen] applicationFrame]];

After you hide the status bar, applicationFrame STILL returns a 320x460 rect (not 320x480). Not sure why, but the following fixes it:

[self.view setFrame: [[UIScreen mainScreen] bounds]];

This gets the whole screen which I lifted from:

Fullscreen UIView with Status bar and Navigation Bar overlay on the top

Second, and in the same line of code, I am not talking to the correct viewController. Since I am in a Navigation Based app, I must talk to the NavigationController. If not, the Navigation Bar never gets the message to move to the top of the screen to cover the space left by the status bar (which resulted in my white space). So one more correction to this line of code:

  [[self.navigationController view] setFrame: [[UIScreen mainScreen] bounds]];

Lastly, after all of this, I can push my modal view with confidence.

Solution 2

I think you may need to recenter your center:

// Set the center point of the view to the center point of the window's content area.      
self.view.center = CGPointMake(160.0, 240.0);

This is the whole init method I use for landscape views and works without an issue. Just make sure to set your nib to the right size too.

// Implement viewDidLoad to do additional setup after loading the view.
- (void)viewDidLoad {
  [super viewDidLoad];

  // Rotate to landscape
  self.view.frame = CGRectMake(0, 0, 480.0, 320.0);
  if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationLandscapeRight) {
    CGAffineTransform transform = self.view.transform;

    // Set the center point of the view to the center point of the window's content area.      
    self.view.center = CGPointMake(160.0, 240.0);

    // Rotate the view 90 degrees around its new center point.
    transform = CGAffineTransformRotate(transform, (M_PI / 2.0));
    self.view.transform = transform;
  }
}
Share:
15,568
Corey Floyd
Author by

Corey Floyd

An iPhone developer, now in Philly!

Updated on June 05, 2022

Comments

  • Corey Floyd
    Corey Floyd almost 2 years

    I think the title is pretty descriptive:

    I perform all 3 of these operations on a view, the result is 20 pixels of white space along the left side of the screen (if holding the iPhone is landscapeLeft).

    I tried to fix it by performing a CGAffineTransFormTranslate(transform, -20, 0)

    That just "slides" the view underneath the whitespace.

    This is starting to feel like a bug, anyone else have this issue?

    To clarify, I am not rotating any views. I am sensing the orientation of the device:

    [[UIDevice currentDevice] orientation]
    

    And if the device (not interface) is in landscape, then:

    [navigationController presentModalViewController:NewView animated:NO]
    

    The transform is to rotate my view (created in IB) to landscape, before displaying it.

    My Transform code:

        CGRect myFrame = CGRectMake(0, 0, 480, 320);
        CGAffineTransform transform = [[self.myPeriodicTableViewController view] transform];
        transform = CGAffineTransformRotate(transform, degreesToRadians(rotationDirection * 90));
        transform =  CGAffineTransformTranslate(transform, -20, 0);
        [[self.myPeriodicTableViewController view] setFrame: myFrame];
        CGPoint center = CGPointMake(myFrame.size.height/2.0, myFrame.size.width/2.0);
        [[self.myPeriodicTableViewController view] setTransform: transform];
        [[self.myPeriodicTableViewController view] setCenter: center];