Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to temporarily lock iPad/iPhone orientation

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"?

like image 381
Eric Avatar asked Jun 18 '10 17:06

Eric


People also ask

How do you freeze orientation on iPad?

If you prefer to use the side switch as a mute button, you can lock your iPad's screen orientation by holding the iPad in either landscape or portrait orientation, swiping up to open Control Center, and selecting the Orientation Lock icon.

How do I keep my iPad screen from turning sideways?

Make sure that Rotation Lock is off: Swipe down from the top-right corner of your screen to open Control Center. Then tap the Rotation Lock button to make sure it's off.

How do I lock the position of the screen on my iPhone?

Simply turn your Apple® iPhone® to change the view. Access the Contol Center by swiping downward from the upper-right corner of the Home or Lock screen. For the iPhone with Touch ID, access the Control Center by touching the bottom of any screen then dragging upward. to lock or unlock screen portrait orientation.


1 Answers

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;
    }
}
like image 181
slf Avatar answered Sep 17 '22 23:09

slf