Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programmatically set device orientation in iOS 7?

I am working on an iPad app, using AutoLayout, where if the user enables a certain mode ("heads-up" mode), I want to support only portrait (or portrait upside down) orientation, and furthermore, if the device is in landscape, I'd like to automatically switch to portrait mode.

In the top view controller, I have the following:

- (NSUInteger) supportedInterfaceOrientations {     if (self.modeHeadsUp) {         return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;     } else {         return UIInterfaceOrientationMaskAll;     } }  - (BOOL) shouldAutorotate {     return TRUE; } 

Based on answers I've seen elsewhere here, the answer seems to be that I should use "application setStatusBarOrientation". Therefore, in the method where the user has selected "heads-up" mode, I have included:

    UIApplication *application = [UIApplication sharedApplication];     [application setStatusBarOrientation:UIInterfaceOrientationPortrait                                 animated:YES]; 

However, this simply doesn't seem to do anything. While I can physically move the device to get it to rotate into portrait, it doesn't do so automatically.

In fact, when in landscape mode after running the above code to attempt to programmatically set the orientation, when I query the application "statusBarOrientation" with the following code, it remains at "4" for landscape:

UIApplication *application = [UIApplication sharedApplication]; int orientation = [application statusBarOrientation]; self.movesTextView.text = [NSString stringWithFormat:@"ORIENTATION %d", orientation]; 

It seemed like maybe autolayout wasn't being triggered with the setStatusBarOrientation, so I attempted to add this code after, to no effect:

    [super updateViewConstraints];     [self.view updateConstraints]; 

I realize Apple wants to leave device orientation in the hands of the user. However, I'd like to be able to support landscape mode when not in "heads-up" mode.

Am I missing something to be able to force orientation change?

like image 653
John Stewart Avatar asked Jan 08 '14 04:01

John Stewart


People also ask

How do I change the orientation of my device in Xcode?

Historically, if you wanted to restrict your iOS app to specific device orientations, you would check or uncheck the various “Device Orientation” options in your project settings. You can find these by selecting your Xcode Project > App Target > “General” tab. Xcode project "Device Orientation" options.

How do you check orientation on iPhone?

Swipe down from the top-right corner of your screen to open Control Center. Tap the Portrait Orientation Lock button to make sure that it's off. Turn your iPhone sideways.


2 Answers

For iOS 7 & 8:

Objective-C:

NSNumber *value = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft]; [[UIDevice currentDevice] setValue:value forKey:@"orientation"]; 

Swift 3+:

let value = UIInterfaceOrientation.landscapeLeft.rawValue UIDevice.current.setValue(value, forKey: "orientation") 

I call it in - viewDidAppear:.

like image 171
Sunny Shah Avatar answered Sep 30 '22 10:09

Sunny Shah


Use this. Perfect solution to orientation problem..ios7 and earlier

[[UIDevice currentDevice] setValue:     [NSNumber numberWithInteger: UIInterfaceOrientationPortrait]         forKey:@"orientation"]; 
like image 40
Arpan Dixit Avatar answered Sep 30 '22 11:09

Arpan Dixit