views:

663

answers:

2

Hi there. My application is quite simple, but I have some problems when it starts. I setted in the Info.plist to be landscaped, but it seems to ignore the order. In fact, when the app is loading the Simulator is landscaped, but then it returns in portrait mode.

This is the hierarchy of the views and controllers:

  • MainViewController (extends UITabBarController just to override shouldAutorotateToInterfaceOrientation:)
    • Three extended UITableViewControllers as tabs (also those have the shouldAutorotateToInterfaceOrientation correctly setted up).

If I kinda force the orientation of the device to Landscape with:

[[UIDevice currentDevice] setOrientation: UIInterfaceOrientationLandscapeRight];

Then for an instant the Simulator flashes in portrait mode, and then it goes landscaped. The problem is that in this way, the auto-rotation animations gets started, which is something I cannot tollerate. I just want a fixed, landscaped application.

Any clues? Am I missing something?

A: 

Try the following. Not sure why it does not work for you

1) set the key UIInterfaceOrientation
to UIInterfaceOrientationLandscapeRight in your .plist file

2) override your UITabBarController shouldAutorotateToInterfaceOrientation() method; in the following the code only deals with tab zero and one, and only with one controller: if you have a navigation controller and you want to control different controllers that may be on the stack, you have to modify the code accordingly

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    BOOL tabZeroController = [[[self.viewControllers objectAtIndex:0] visibleViewController] isKindOfClass:[YourTabZeroTableViewController class]];

    BOOL tabOneController = [[[self.viewControllers objectAtIndex:1] visibleViewController] isKindOfClass:[YourTabOneTableViewController class]];


    if(self.selectedIndex == 0 && tabZeroController)
     return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);

    if(self.selectedIndex == 1 && tabOneController)
     return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);

    return NO;

}

2) setting

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

in your delegate's applicationDidFinishLaunching() method is only required for the simulator, not on a device

3) add the following shouldAutorotateToInterfaceOrientation(method) in your controllers

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

4) run the app on your device and verify that it works properly by using the Hardware menu item Rotate Left and Rotate Right. You should see the display in landscape-mode.

unforgiven
I think I'm missing something in your UITabBarController shouldAutorotateToInterfaceOrientation implementatio. So you want to return YES only if the viewcontroller of the currently selected tab is one of the viewcontrollers I setted for the tabbarcontroller?
Stefano Verna
Yes, basically that's the idea if you want to have a mixed behaviour in which some of your tab controllers are landascape only, while others may behave differently. If this is not your case, in that method you simply return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
unforgiven
So that's exactly what I'm doing now.. that's so strange. I'll keep on investigate.
Stefano Verna
It's really strange indeed. It works on both my iPhone 3G and 3GS.
unforgiven