Lock / unlock orientation changes in iOS

I am developing an application in iOS. I need to lock or unlock the orientation by pressing a button.

I also checked this link.

I need to lock the screen with the click of a button, if a click again means the need to unlock.

I tried using this code but not useful.

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


-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationPortrait | UIInterfaceOrientationLandscapeLeft | UIInterfaceOrientationLandscapeRight |UIInterfaceOrientationPortraitUpsideDown;
}

Any help is appreciated.

+3
source share
2 answers

If you just want to block the rotation when you press the button, you can use.

- (BOOL)shouldAutorotate
{
   if (autorotate) {
     return YES;
   } 
   else
   {
     return NO;
   }
}

because shouldAutorotateToInterfaceOrientation is now DEPRECATED.

Use of this orientation will be locked in the current orientation.

+2
source

- , ,

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

Iphone 7.1.1, , iOS 2.0,
Ref: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/occ/instm/UIDevice/endGeneratingDeviceOrientationNotifications

0