iPhone: In landscape-only, after first addSubview, UITableViewController doesn't rotate properly

Posted by Clay Bridges on Stack Overflow See other posts from Stack Overflow or by Clay Bridges
Published on 2009-10-27T16:38:01Z Indexed on 2010/03/25 19:13 UTC
Read the original article Hit count: 612

Filed under:
|

A minimal illustrative Xcode project for this is available on github.

On my UIWindow, when I add second (and subsequent) UITableView's as subviews, they do not rotate properly, and thus appear sideways. This is only tested in the Simulator. Here's a little code for you:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
 ShellTVC* viewA = [[ShellTVC alloc] initWithTitle:@"View A"];
 ShellTVC* viewB = [[ShellTVC alloc] initWithTitle:@"View B"];

 // The first subview added will rotate to landscape correctly. 
 // Any subsequent subview added will not.

 // You may try this by various commentings and rearranging of these two statements.

 [window addSubview:[viewA tableView]];
 [window addSubview:[viewB tableView]];

    [window makeKeyAndVisible];
}

viewB appears sideways. Comment out the addSubview for viewB, and viewA appears correctly. Do that for viewA only, and viewB appears correctly.

I am not creating these UITableViewControllers via NIBs, though the UIWindow is.

In case you are wondering, ShellTVC is-a UITableViewController, and implements this method:

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

Also, I have set the UIInterfaceOrientation in the plist file to UIInterfaceOrientationLandscapeLeft.

Probably related -- and unanswered -- SO questions here and here.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about landscape