Iphone – UINavigationController Auto-Rotation to Landscape does not update UIView frame

cocoacocoa-touchiphoneuser interface

I'm working on an application that operates entirely in landscape mode (UIStatusBarHidden=YES and UIInterfaceOrientation=UIInterfaceOrientationLandscapeRight). I'm using a NavigationController, with my rootViewController (MainViewController) setup like this:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

In my MainViewController, I'm loading the view like this:

- (void)loadView {
  CGRect frame = [[UIScreen mainScreen] applicationFrame]; // This returns a portrait frame
  MainView *view = [[MainView alloc] initWithFrame:frame];
  self.view = view;
  [view release];
}

Then in MainView, I'm loading subviews like this:

- (id)initWithFrame:(CGRect)frame {
  if (self = [super initWithFrame:frame]) {
    self.pdfView = [[PDFView alloc] initWithFrame:frame];
    [self addSubview:pdfView];
  }
   returnself;
}

My issue is that MainView renders correctly in landscape mode (despite the frame passed into [MainView initWithFrame:] being in portrait), while the child PDFView renders into a portrait frame. I have also experimented with applying transforms in my [UIView initWithFrame:] implementations like this:

view.center = CGPointMake(view.frame.size.height/2.0, view.frame.size.width/2.0);
view.transform = CGAffineTransformRotate(view.transform, (M_PI/2.0));

But that doesn't seem to work since the frame value is incorrect. Ignoring the frame and creating one manually like this seems to work in some cases:

CGRectMake(0.0, 0.0, 480.0, 320.0)

But obviously doing manual adjustments like that is less than ideal.

Any ideas?

Best Answer

I don't know if this helps in your case, but make sure all your views and controls support autorotate. The UINavigationController does not, neither does the UITabBarController. The Tab Bar controller will stop supporting it if any of it's child views do not support autorotate. I had to create my own UINavigationController and make it inherit from UINavigationController and then implement the autorotate. Since you are doing your application completely in landscape I don't know if this helps or not, but it might get you started down a path to your solution.

Related Topic