3

I am developing a application in iOS. I need to lock or unlock the orientation with a button click.

I have checked this link also.

I need to lock the screen on single click button, if click again means need to unlock.

I have tried using this code but no useful.

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


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

Any help is appreciated.

Community
  • 1
  • 1
KethanKumar
  • 716
  • 5
  • 17
  • I do not know if Apple has fixed it already, but shouldAutoRotateToInterfaceOrientation worked perfectly in IOS 5 and was completely useless from IOS6 onwards. I ended up fixing the orientation for my entire app because I couldn't control it anymore from page to page. I'd be happy to know if this is already resolved. – Paulo Feb 25 '14 at 05:15
  • 1
    @ paulo it is not that it doesnt work, from ios 6 onwards supportedInterfaceOrientations is used instead. – Pochi Feb 25 '14 at 05:19
  • From iOS 6, orientation changes relay on the container controller such as navigation controller or tab bar controller. You could override container controller methods to control the orientation changes page by page. [Refer this](http://stackoverflow.com/questions/15004271/prevent-autorotate-for-one-view-controller/15004471#15004471) – Anil Varghese Feb 25 '14 at 05:21
  • What is the behavior you expect? Pressing a button rotates the screen immediately? Lock the orientation for ANOTHER screen? For the whole app? Isnt this what the lock button is for (on the device)? – Pochi Feb 25 '14 at 05:22
  • @Chiquis For the whole app i need to lock the screen. When Button clicks it should not rotate, if i click again it should rotate. Lock button means PUSH Button need to give functionality to this. – KethanKumar Feb 25 '14 at 05:25

2 Answers2

2

If you want to just lock rotation on button click then you can use.

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

because shouldAutorotateToInterfaceOrientation is now DEPRECATED.

Using this orientation will be locked in current orientation.

Homam
  • 5,018
  • 4
  • 36
  • 39
Gourav Gupta
  • 41
  • 1
  • 7
  • being deprecated doesnt mean it should not be implemented, it means that if he plans to ONLY support ios 6+ then its not needed, else he has to implement shouldAutorotateToInterfaceOrientation to support previous ios versions. – Pochi Feb 25 '14 at 05:36
  • @Chiquis yes you are right. but if you want to support ios6+ then it will not work for you. One more problem that ios 6 have, in ios 6 orientation doesn't support with navigation, in this case you have to override orientations. – Gourav Gupta Feb 25 '14 at 05:40
  • What i mean is that you should implement both if you want to support the broadest iOS versions. I didn't understand the part about ios 6 not supporting with navigation. – Pochi Feb 25 '14 at 05:43
1

I have discover something interesant, you can do this to lock the rotation

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

and then enabled again

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

Pretty easy

Tested on real Iphone device with 7.1.1, the doc say it's available since iOS 2.0 so must work on all devices
Ref: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDevice_Class/Reference/UIDevice.html#//apple_ref/occ/instm/UIDevice/endGeneratingDeviceOrientationNotifications

Sulfkain
  • 5,082
  • 1
  • 20
  • 38