problems programmatically creating UIView on iPad App

Posted by user3871 on Game Development See other posts from Game Development or by user3871
Published on 2010-12-08T13:46:55Z Indexed on 2011/02/04 7:34 UTC
Read the original article Hit count: 238

Filed under:

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 addSubview:viewController.view];
    [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.

© Game Development or respective owner

Related posts about ipad