Start in Landscape, stay in Landscape

Oh, this one took forever.  The issue is you want to have an app start in landscape and then have every new ViewController also be in Landscape.  Internet searches will only tell you the first half of the story.  “Initial Interface orientation” of LandscapeLeft.  “Status bar is initially hidden” to true.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

But then as soon as you pop off your starting VC and try to add a new one, you get in the State From Hell.  Your VC has its origin in the top right corner, and the text runs downwards.  If you turn on the status bar, it is right.  If you break point in the ShouldRotate, it is right.  If you ask for the frame, it is right.  The screaming, it does nothing.

Hopefully you have the same problem I had so this will save you.  You can’t make a Window app play in landscape correctly.  I always start projects bare bones and instantiate my VC’s by hand.  (I still use nibs for their actual layout.)  And I hang them on my window.  Don’t.  Make a VC that you will never do anything with and hang everybody on it.

@interface Windowshade : UIViewController
{
}
@end

@interface GameAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
Windowshade *m_Windowshade;

UIViewController *m_ActiveVC;

- (void)applicationDidFinishLaunching:(UIApplication *)application
{
m_Windowshade = [[Windowshade alloc]init];
[m_Windowshade retain];

m_Windowshade.view.autoresizesSubviews = YES;
m_Windowshade.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

[window addSubview:m_Windowshade.view];
[window makeKeyAndVisible];

m_ActiveVC = [[MainScreen alloc] initWithNibName:@”MainScreen” bundle:nil];
[m_ActiveVC retain];

[m_Windowshade.view addSubview:m_ActiveVC.view];

@implementation Windowshade

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

@end

Tags:

About Graham Smallwood

My name is Graham Smallwood, and I have over 10 years of programming experience and 6 published million-selling games. I work full time as an iOS developer, and am always looking for contract work to do in my spare time.