項目需要,只有某個界面需要橫屏顯示,其它全只支持豎屏顯示即可,網上資料很多,但是試過都不好用,最后發現是因為我的項目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; }
親測好用~