4

If a view is added to the window, orientation is set as portrait even if the device is in landscape. If the view is added in the app delegate, application:didFinishLaunchingWithOptions: method, then it works correctly. But if the view is added later it does not.

As an example, I have a routine to switch views. Simplest form is:

- (void)switchToNewViewController:(UIViewController *)newViewController { 
 if ([[window subviews]count]!=0) {
  [[[window subviews]objectAtIndex:0] removeFromSuperview];
 }
 [window addSubview:newViewController.view];
}

IF this is called from within didFinishLaunching, orientation is correct. If it is not, orientation is portrait.

Simplest case is within didFinishLaunching I have the following two lines

// The following line works
[self switchToNewViewController:fullScreenViewController];

// The following line which delays the method call until later results
// in incorrect orientation 
[self performSelector:@selector(switchToNewViewController:) withObject:fullScreenViewController afterDelay:0.1];

Is there a way to make the view have the proper orientation?

David
  • 2,770
  • 5
  • 35
  • 43

1 Answers1

1

Make sure your shouldAutorotateToInterfaceOrientation in the view controllers has the right logic

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return YES; //this supports all orientations
}

If you are linking stuff in InterfaceBuilder also make sure both the view and the viewcontroller are configured to the initial orientation you like. (There is a little arrow in the top right corner to rotate views and view controllers)

If you still have problems, are you using a UINavigationController or similar? UINavigationController needs to be subclassed and shouldAutorotateToInterfaceOrientation implemented if you want to support something other than portait.

Tyler Zale
  • 634
  • 1
  • 7
  • 23
  • This was partially the problem. As I recall, Apple dev support came up with two issues. Its important that all views being swapped support the same orientation, and only one controller should control the screen at a time. There were also known issues with iOS3.2 that were corrected in later releases. – David Nov 26 '11 at 01:49