views:

412

answers:

1

At a certain point in my application, I would like to temporarily disable the rotation feature - effectively "lock" the orientation in code, similar to what the lock toggle switch does on the iPad in hardware.

I'm currently doing:

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]

and when I'm through, I call:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]

While this all appears to work, the API documentation indicates there should always be a begin followed by an end - and I'm doing it essentially in reverse. Is there a more appropriate API for handling the "lock orientation"?

+5  A: 

I think it's as simple as not responding to the orientation changes from your controller. You can conditionally return true or false depending on the parameters you are passed, you don't always have to return true.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (rand()%2 == 0) { // evaluate if I should allow rotate
      return YES;
    } else {
      return NO;
    }
}
slf
Thanks - works great!
Eric