简体   繁体   中英

How to allow a particular view controller to support for both landscape and portrait orientation

My application is navigation based application which is supporting iOS 6. Except a view controller all others will support only portrait mode. Particular view controller alone has to support both landscape and portrait orientations.

I searched lot there are tons of questions for this but none of the answer is suitable for me. If some one knows, kindly guide me

I set the orientation as Portait in Project -> Targets -> Summary -> Supported orientation

First you should use methods for the iOS6 presented in UIViewController documentation if you are making your app for iOS6. Orientation method like shouldAutorotateToInterfaceOrientation is deprecated in iOS6, alternate method for iOS6 is shouldAutoRotate . You should only use the old method if your app is supporting also iOS5.

Second If you are using UINavigationcontroller in your application and you need to have different interface orientations then navigationController could mess up the interface orientation in the application. Possible solution (worked for me) is to implement a custom UINavigationController and override the interface orientation methods within that custom UINavigationController class, this will make your viewControllers rotate according to the orientation you set because your controllers are pushed from the UINavigationController . Don't forget to add those methods in your particular viewController also.

CustomNavigationController.h

@interface CustomNavigationController : UINavigationController
@end

CustomNavigationController.m

@implementation CustomNavigationController

//overriding shouldRotate method for working in navController
-(BOOL)shouldAutorotate
{  
  return   [self.topViewController shouldAutorotate];   
}

-(NSUInteger)supportedInterfaceOrientations
{
   return [self.topViewController supportedInterfaceOrientations];
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
   return [self.topViewController preferredInterfaceOrientationForPresentation];
}

I really think that the correct way to do it is to set both landscape and portrait as supported orientations, and not allowing a change in the orientation on the VC you don't want to rotate, by returning NO on the

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

if the orientation that was passed to the method was not supposed to be supported.

As mentioned in the comments, this method can no longer be used. You should use :

-(BOOL) shouldAutorotate

And find out the current orientation inside this function and returning NO if you don't want to rotate.

Don't forget to allow your application different Orientations!

otherwise nothing from above will work

Set it in project target on General under Deployment Info section:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM