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

给你一囗甜甜゛ 提交于 2019-12-10 22:12:21

问题


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


回答1:


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];
}



回答2:


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.




回答3:


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:



来源:https://stackoverflow.com/questions/15808532/how-to-allow-a-particular-view-controller-to-support-for-both-landscape-and-port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!