# problems programmatically creating UIView on iPad App

I have been struggling with this problem for a few days.

My iPad app is designed to be a portrait game. To satisfy Apple's expection, I also support landscape mode. When it goes into landscape mode, the game goes into a letterbox format with back borders on the sides.

My problem is I am creating the UIWindow and UIView programmatically. For some unkown reason, the touch controls are "locked" in to think I'm always in landscape mode. And even though visually in portrait mode everything looks correct, the top and bottom of the screen does not respond to touch.

To summarize how I am setting this up, let me provide the skeletal framework of what I'm doing:

in main.cpp:

int retVal = UIApplicationMain(argc, argv, nil, @"derbyPoker_ipadAppDelegate");


In the delegate, I am doing this:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGFloat scale = [[ UIScreen mainScreen] scale ];

m_device_width = screenBounds.size.width;
m_device_height = screenBounds.size.height;
m_device_scale = scale;        // Everything is built assuming 640x960

window = [[ UIWindow alloc ] initWithFrame:[[UIScreen mainScreen] bounds]];
viewController = [ glView new ];
[self doStateChange:[blitz class]];

return YES;
}


The last bit of code sets up the UIView...

      - (void) doStateChange: (Class) state{
viewController.view = [[state alloc] initWithFrame:CGRectMake(0, 0, m_device_width, m_device_height) andManager:self];
viewController.view.contentMode = UIViewContentModeScaleAspectFit;
viewController.view.autoresizesSubviews = YES;
[window makeKeyAndVisible];
}


The problem seems to related to the line viewController.view.contentMode = UIViewContentModeScaleAspectFit;

If I remove that line, touch works correctly in portrait mode. But the negative is when I'm landscape mode, the game stretches incorrectly. So That's not a option.

The frustrating thing is, when I originally had this set up with a NIB file, it worked fine. I have read through the docs about UIWindow, UIViewController and UIView and have tried about everything to no avail.

Any help would be greatly appreciated.

• You don't have to support both portrait and landscape for your game. It's just if you write a game that works in portrait mode, it should also work in portrait upside-down mode. Dec 8 '10 at 15:19
• Agreed, Plus most games don't need this anyway. There are many many games that don't handle both orientations let alone both of the same orientation. Dec 9 '10 at 23:23
• @FuzzYspo0N: It doesn't matter so much for iPhone, but not supporting the upside-down orientation in an iPad app can be a reason for Apple to reject your app. Jul 6 '11 at 14:36
• @bummzack, I was referring to both landscape and portrait. Not both types of landscape. That is a no brainer. Sep 10 '11 at 20:15

Here there is a tutorial of how achieve this:

I think that this approach uses a MVC approach and is the view which makes the transformation for you ( graphics with animation included and input ).

On the other hand, I remind that if you want do this rotation with programation the view doesn't rotate and you need transform the coordinates that receives from the iOS.

In that book there is an example to do programatically ( with animation also ):

Name: iPhone 3D Programming
Author: Philip Rideout

I have seen the two approaches and I recommend you to use the MVC approach, because when the application starts, it's impossible know the orientation with programmation ( the first accelerometer event doesn't reach ) and the image may be inverted for the first time. With MVC you see the image in correct orientation the first time.

There's no requirement to support both orientations. But if you do, you can change the content mode in the -(void)willAnimateRotationToInterfaceOrientation method. I'm on an ipad at the moment or i would post more specific code, but I'll come back later if someone hasn't posted more.

I submitted this bug to Apple last month and with they sent me an email the other day that said this bug was fixed with the latest release of iOS. I believe its fixed after iOS 4.3