Force IOS 6 force device orientation to landscape

来源:互联网 发布:优化方案答案物理答案 编辑:程序博客网 时间:2024/05/01 05:26


http://stackoverflow.com/questions/12640870/ios-6-force-device-orientation-to-landscape


Ok, folks, I will post my solution.

What I have:

  1. A view based application, with several view controllers. (It was navigation based, but I had to make it view based, due to orientation issues).
  2. All view controllers are portrait, except one - landscapeLeft.

Tasks:

  1. One of my view controllers must automatically rotate to landscape, no matter how the user holds the device. All other controllers must be portrait, and after leaving the landscape controller, the app must force rotate to portrait, no matter, again, how the user holds the device.
  2. This must work as on IOS 6.x as on IOS 5.x

Go!

Define macros that will detect the IOS version:

Create an NSObject class "Constants", delete Constants.m, you need Constants.h only. Delete all Constants.h's code and write this:

#define IOS_OLDER_THAN_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] < 6.0 )#define IOS_NEWER_OR_EQUAL_TO_6 ( [ [ [ UIDevice currentDevice ] systemVersion ] floatValue ] >= 6.0 )

Import Constants.h in all your .m files where you will override autorotation methods.

In all your PORTRAIT view controllers override autorotation methods like this:

#ifdef IOS_OLDER_THAN_6- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{    return (toInterfaceOrientation == UIInterfaceOrientationPortrait);}#endif#ifdef IOS_NEWER_OR_EQUAL_TO_6-(BOOL)shouldAutorotate {    return YES;}- (NSUInteger)supportedInterfaceOrientations {    return UIInterfaceOrientationMaskPortrait;}#endif

You can see the 2 approaches: one for IOS 5 and another For IOS 6.

The same for your LANDSCAPE view controller, with some additions and changes:

#ifdef IOS_OLDER_THAN_6- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{    [image_signature setImage:[self resizeImage:image_signature.image]];    return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft);}#endif#ifdef IOS_NEWER_OR_EQUAL_TO_6-(BOOL)shouldAutorotate {    return YES;}- (NSUInteger)supportedInterfaceOrientations {    [image_signature setImage:[self resizeImage:image_signature.image]];    return UIInterfaceOrientationMaskLandscapeLeft;}#endif
原创粉丝点击