Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set device (UI) orientation programmatically?

Tags:

Would like everything on the screen (UI) to be able to rotate from landscape left to right or vica versa.

How do I go about doing this? Is this private?

I know that

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {} 

lets you say which orientations the UI can be, but is there a way to force only one at a time?

Cheers

like image 277
Sam Jarman Avatar asked Dec 02 '10 01:12

Sam Jarman


People also ask

How do I force landscape in iOS?

On an iPhone without a Home button, swipe down from the top-right corner of the screen instead. Here, tap on the rotation lock icon (which looks like a lock with a circular arrow) to turn it on or off.

How do I find the orientation of my device?

If the activity locks the display ( android:screenOrientation="portrait" ), this method will return the same value irrespective of how the user rotates the device. In that case you'd use the accelerometer or the gravity sensor to figure out orientation properly.

How do I change the orientation of my apps in Appium?

You can change the screen orientation of an Android or iOS device to set your application in portrait or landscape mode during the execution of the test. Use the capability deviceOrientation to change the screen orientation.


2 Answers

In iOS [[UIDevice currentDevice] setDeviceOrientation:UIDeviceOrientationSomeOrientation] method is absent. But we can rotate a view with status bar, for this:

- (void)showAlbum {     // check current orientation     if ([[UIApplication sharedApplication] statusBarOrientation] != UIInterfaceOrientationLandscapeLeft) {         // no, the orientation is wrong, we must rotate the UI         self.navigationController.view.userInteractionEnabled = NO;         [UIView beginAnimations:@"newAlbum" context:NULL];         [UIView setAnimationDelegate:self];         // when rotation is done, we can add new views, because UI orientation is OK         [UIView setAnimationDidStopSelector:@selector(addAlbum)];         // setup status bar         [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft  animated:NO];         // rotate main view, in this sample the view of navigation controller is the root view in main window         [self.navigationController.view setTransform: CGAffineTransformMakeRotation(M_PI / 2)];         // set size of view         [self.navigationController.view setFrame:CGRectMake(0, 0, 748, 1024)];         [UIView commitAnimations];     } else {         [self addAlbum];     }  } 
like image 81
UIBuilder Avatar answered Sep 20 '22 01:09

UIBuilder


I've had success with this:

 if (self.interfaceOrientation != UIInterfaceOrientationPortrait) {     // http://stackoverflow.com/questions/181780/is-there-a-documented-way-to-set-the-iphone-orientation     // http://openradar.appspot.com/radar?id=697     // [[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait]; // Using the following code to get around apple's static analysis...     [[UIDevice currentDevice] performSelector:NSSelectorFromString(@"setOrientation:") withObject:(id)UIInterfaceOrientationPortrait]; } 
like image 32
Chris Avatar answered Sep 23 '22 01:09

Chris