0

I'm helping out on an app for the iPad on Xcode 4. I know there is the

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

that should return YES if you want to support all orientations. Previously, the app was stuck in Landscape mode. I am trying to change the views to support all orientations. So I changed the occurrences of that method to return YES instead of only YES for the LandscapeLeft and LandscapeRight orientation. However, when I try to do this:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
            NSLog(@"%i, %s", self.interfaceOrientation, __FUNCTION__);
}

I always get 3 or 4 in the console which are the two Landscape orientation. I checked the Summary page when you click on the project icon, and all device orientations are supported. In the .plist file, all orientations are listed. Is there another place that this could have been set that I'm overlooking? Thanks.

Crystal
  • 28,460
  • 62
  • 219
  • 393
  • Don't know if this will help or no, but its somewhere to start - http://stackoverflow.com/questions/2868132/shouldautorotatetointerfaceorientation-wont-work. – Perception Aug 08 '11 at 19:43
  • Sounds like there’s something else going on here. Try listening for the global NSNotification of the orientation changing, then comparing with your logs. – David Cairns Aug 08 '11 at 20:58

1 Answers1

0

I had the same problem working with custom navigation controllers that present child view controllers. In my case, I'm using a table view to present items in a grid, and I needed my table view cells to display different numbers of items based on the orientation. Here's the work around I found:

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    // we have to use self.view.window.rootViewController.interfaceOrientation because for some reason
    // self.interfaceOrientation isn't updating.  That's what I get for using custom navigation, I guess.
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];
    if (!self.isViewLoaded) {
        return;
    }
    if (self.view.window) {
        if (UIInterfaceOrientationIsPortrait(self.view.window.rootViewController.interfaceOrientation) != UIInterfaceOrientationIsPortrait(fromInterfaceOrientation)) {
            // if we are rotating between portrait and landscape, we'll have to reload the data
            [self.tableView reloadData];
        }
    } else {
        self.needsReloadData = YES;
    }
}

By reading the orientation from the root view controller of the window, you have a reliable way of determining the actual orientation of the device. Unfortunately, if the view controller's view has been removed from the current window, self.view.window will return nil, so in that case I have to raise a flag and handle it in viewDidAppear.

Jose Ibanez
  • 3,325
  • 3
  • 28
  • 33