Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a documented way to set the iPhone orientation?

I have an app where I would like to support device rotation in certain views but other don't particularly make sense in Landscape mode, so as I swapping the views out I would like to force the rotation to be set to portrait.

There is an undocumented property setter on UIDevice that does the trick but obviously generates a compiler warning and could disappear with a future revision of the SDK.

[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; 

Are there any documented ways to force the orientation?

Update: I thought I would provide an example as I am not looking for shouldAutorotateToInterfaceOrientation as I have already implemented that.

I want my app to support landscape and portrait in View 1 but only portrait in View 2. I have already implemented shouldAutorotateToInterfaceOrientation for all views but if the user is in landscape mode in View 1 and then switches to View 2, I want to force the phone to rotate back to Portrait.

like image 526
Dave Verwer Avatar asked Oct 08 '08 08:10

Dave Verwer


People also ask

How does iPhone know to rotate?

To detect a phone's orientation, the accelerometer communicates with the gyroscope and the magnetometer. Therefore, when a phone rotates, the accelerometer detects changes in acceleration and then communicates with the gyroscope.

How do I change my orientation settings?

Select the Start button, then type settings. Select Settings > System > Display, and choose a screen orientation from the drop-down list next to Display orientation.


2 Answers

This is long after the fact, but just in case anybody comes along who isn't using a navigation controller and/or doesn't wish to use undocumented methods:

UIViewController *c = [[UIViewController alloc]init]; [self presentModalViewController:c animated:NO]; [self dismissModalViewControllerAnimated:NO]; [c release]; 

It is sufficient to present and dismiss a vanilla view controller.

Obviously you'll still need to confirm or deny the orientation in your override of shouldAutorotateToInterfaceOrientation. But this will cause shouldAutorotate... to be called again by the system.

like image 185
Josh Avatar answered Oct 19 '22 23:10

Josh


If you want to force it to rotate from portrait to landscape here is the code. Just note that you need adjust the center of your view. I noticed that mine didn't place the view in the right place. Otherwise, it worked perfectly. Thanks for the tip.

if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation)){          [UIView beginAnimations:@"View Flip" context:nil];         [UIView setAnimationDuration:0.5f];         [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];          self.view.transform = CGAffineTransformIdentity;         self.view.transform = CGAffineTransformMakeRotation(degreesToRadian(90));         self.view.bounds = CGRectMake(0.0f, 0.0f, 480.0f, 320.0f);         self.view.center = CGPointMake(160.0f, 240.0f);          [UIView commitAnimations]; } 
like image 39
Michael Gaylord Avatar answered Oct 19 '22 23:10

Michael Gaylord