Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autorotation lock, programmatically

Does anybody know if there is a possibility to lock autorotation of iPhone programmatically for just one view? I want to make some kind of help with semi-transculent view, but I want to support only landscape orientation even all other views can rotate. So I wish to lock rotation when this view is on the top.

tnx

EDIT: More details: one UIController has 7 UIView...and I wish to lock the autorotation just when the last one occurs on the top.

like image 776
Vanya Avatar asked May 24 '11 09:05

Vanya


People also ask

What is orientational lock?

If you don't want the screen to switch between portrait and landscape when you move the device, you can lock the screen orientation. To do this, swipe down from the right side of the top panel. Hold the device in the orientation in which you want it locked.

How do I remove the orientation lock?

Swipe up from the bottom edge of your screen to open Contol Center. Tap the Portrait Orientation Lock button to make sure that it's off. Turn your iPhone or iPod touch sideways.


3 Answers

This question was asked over a year ago but the accepted method is now deprecated in iOS 6 so if anybody is interested in doing this in iOS 6 then you need to use supportedInterfaceOrientations on the topmost controller.

Imagine you have a setup like this...

  • Tab Bar Controller
    • Navigation Controller
      • View Controller

... then you need to set the supportedInterfaceOrientations method on the tab bar controller.

Create a subclass of the tab bar controller (or navigation controller if that is at the top) and set these methods in it...

- (BOOL)shouldAutorotate {
    //Use this if your root controller is a navigation controller
    return self.visibleViewController.shouldAutorotate;
    //Use this if your root controller is a tab bar controller
    return self.selectedViewController.shouldAutorotate;
}

- (NSUInteger)supportedInterfaceOrientations {
    //Navigation Controller
    return self.visibleViewController.supportedInterfaceOrientations;
    //Tab Bar Controller
    return self.selectedViewController.supportedInterfaceOrientations;
}

... then in your individual view controllers you can set the properties you want...

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations{
    //return supported orientation masks
    return UIInterfaceOrientationMaskLandscape;
}
like image 116
klkl Avatar answered Oct 23 '22 02:10

klkl


Use the following...

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}
like image 41
Simon Lee Avatar answered Oct 23 '22 02:10

Simon Lee


You can attach it to the window. After the view is loaded, do

[self.view.window addSubview:yourStaticView];
[self.view.window bringSubviewToFront:yourStaticView]; // Do only if necessary

Remove it when leaving this view. Probably in viewWillDisappear: or viewDidDisappear:.

like image 3
Deepak Danduprolu Avatar answered Oct 23 '22 03:10

Deepak Danduprolu