如何允许特定的视图控制器同时支持横向和纵向

如何允许特定的视图控制器同时支持横向和纵向

问题描述:

我的应用程序是支持iOS 6的基于导航的应用程序.除视图控制器外,其他所有应用程序将仅支持纵向模式.仅特定的视图控制器必须同时支持横向和纵向.

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.

第二个如果您在应用程序中使用UINavigationcontroller并且需要具有不同的界面方向,则navigationController可能会弄乱应用程序中的界面方向.可能的解决方案(对我有用)是实现自定义UINavigationController并覆盖该自定义UINavigationController类中的界面方向方法,这将使您的viewControllers根据您设置的方向旋转,因为您的控制器是从.不要忘记也将这些方法添加到您的特定viewController中.

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