A for Alpha
A for Alpha

Reputation: 2912

How to set device orientation programmatically in iOS

I have a ebook reader app where in, i need to programmatically adjust the device orientation according to the layout of the page... i.e, The device should support all orientations for some pages and only landscape for some other pages. Is there anyway in which i can force a orientation change programatically

Upvotes: 1

Views: 9339

Answers (6)

Hanton
Hanton

Reputation: 686

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

This can set the device orientation to LandscapeRight programmatically.

Upvotes: 1

SamSmart
SamSmart

Reputation: 355

This works for me on Xcode 6 & 5.

- (BOOL)shouldAutorotate {return YES;}
- (NSUInteger)supportedInterfaceOrientations {return (UIInterfaceOrientationMaskPortrait);}

Upvotes: 0

Kalle
Kalle

Reputation: 13346

Note that the behavior for this has changed as of iOS 6.0. For further info, see: Autorotate in iOS 6 has strange behaviour

The gist is that should/will/did/etc have been deprecated and have been replaced with a cleaner, more dynamic implementation. Which we should all be thankful for.

Upvotes: 1

Guntis Treulands
Guntis Treulands

Reputation: 4762

Actually you can force any rotation. At least I was able to do it. Solution I used: https://stackoverflow.com/a/10146270/894671

Upvotes: 0

Marcelo Alves
Marcelo Alves

Reputation: 1846

You can use the answer from jbat100 plus a call to setStatusBarOrientation:animated in UIApplication to achieve the effect.

Upvotes: 1

jbat100
jbat100

Reputation: 16827

You cannot force the device orientation to some value, the system tells you the device's orientation, not the opposite. You can rotate your views using CGAffineTransform and such (a related post here).

Upvotes: 0

Related Questions