项目需要,只有某个界面需要横屏显示,其它全只支持竖屏显示即可,网上资料很多,但是试过都不好用,最后发现是因为我的项目UIViewController外层是UINavigationVeiwController,只在UIViewController重载supportedInterfaceOrientations与shouldAutorotate 方法是不行的。
下面说明具体设置步骤:(参考http://www.cocoachina.com/bbs/read.php?tid-244095.html)
Step 1:Info.plist中设置Supported interface orientations 为所有支持的方向,我是这样设置的:
Step 2:在自定义的navigationViewController中添加属性:
@property (nonatomic, assign) BOOL supportLandscape;
初始化:
- (id)init { if (self = [super init]) { [self setup]; } return self; } - (void)setup { //其他设置 ... self.supportLandscape = NO; }
重载supportedInterfaceOrientations方法:
- (UIInterfaceOrientationMask) navigationControllerSupportedInterfaceOrientations:(UINavigationController *) navigationController{ if(self.supportLandscape){ return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeRight; }else{ return UIInterfaceOrientationMaskPortrait; } }
Step 3:我的项目中所有viewController都是由navigationController控制的,所以,只需要在需要支持横屏的viewController中进行设置就好了:
-(void) viewDidAppear:(BOOL) animated{ [SlideNavigationController sharedInstance].supportLandscape = YES; } -(void) viewDidDisappear:(BOOL) animated{ [SlideNavigationController sharedInstance].supportLandscape = NO; }
亲测好用~