標籤:
1、屏蔽AppDelegate下面的旋轉螢幕方法
#pragma mark - 旋轉螢幕的//- (UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window//{// return UIInterfaceOrientationMaskPortrait;//}
2、對UINavigationController和UITabBarController寫兩個擴充類別,此東西不需要在具體的ViewController中引用
UINavigationController+Autorotate.h
//// UINavigationController+Autorotate.h// fitmiss//// Created by bill on 15/12/16.// Copyright © 2015年 lear. All rights reserved.//#import <UIKit/UIKit.h>@interface UINavigationController (Autorotate)@end
UINavigationController+Autorotate.m
//// UINavigationController+Autorotate.m// fitmiss//// Created by bill on 15/12/16.// Copyright © 2015年 lear. All rights reserved.//#import "UINavigationController+Autorotate.h"@implementation UINavigationController (Autorotate)- (BOOL)shouldAutorotate{ return [self.topViewController shouldAutorotate];}- (NSUInteger)supportedInterfaceOrientations{ return [self.topViewController supportedInterfaceOrientations];}- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return [self.topViewController preferredInterfaceOrientationForPresentation];}@end
UITabBarController+Autorotate.h
//// UITabBarController+Autorotate.h// fitmiss//// Created by bill on 15/12/16.// Copyright © 2015年 lear. All rights reserved.//#import <UIKit/UIKit.h>@interface UITabBarController (Autorotate)@end
UITabBarController+Autorotate.m
//// UITabBarController+Autorotate.m// fitmiss//// Created by bill on 15/12/16.// Copyright © 2015年 lear. All rights reserved.//#import "UITabBarController+Autorotate.h"@implementation UITabBarController (Autorotate)- (BOOL)shouldAutorotate{ return [self.selectedViewController shouldAutorotate];}- (NSUInteger)supportedInterfaceOrientations{ return [self.selectedViewController supportedInterfaceOrientations];}- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return [self.selectedViewController preferredInterfaceOrientationForPresentation];}@end
3.在使用的ViewController中的再次重寫這三個方法,可以在根ViewController中重寫如下方法,就能實現豎屏
- (BOOL)shouldAutorotate{ return NO;}
摘自網上網友的回複內容,如下:
以下方法僅對deploy target大於等於iOS6的工程有效,如果題主的應用需要支援iOS5(默哀),請pass。
- 在info.plist中設定方向,包含你需要的所有方向,以題中意,UpSideDown和LandScapeLeft;
- 繼承UITabBarController,override以下三個方法
- (BOOL)shouldAutorotate{ return [self.selectedViewController shouldAutorotate];} - (NSUInteger)supportedInterfaceOrientations{ return [self.selectedViewController supportedInterfaceOrientations];} - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return [self.selectedViewController preferredInterfaceOrientationForPresentation];}
- 繼承UINavigationController,override和UITabBarController中相同的方法,將selectedViewController改為topViewController
- (BOOL)shouldAutorotate{ return [self.topViewController shouldAutorotate];} - (NSUInteger)supportedInterfaceOrientations{ return [self.topViewController supportedInterfaceOrientations];} - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{ return [self.topViewController preferredInterfaceOrientationForPresentation];}
- 在真正實現介面的ViewController裡,override上面這三個方法,override規則如下:
preferredInterfaceOrientationForPresentation表示viewController初始顯示時的方向;
supportedInterfaceOrientations是在該viewController中支援的所有方向;
shouldAutorotate表示是否允許旋屏。
流程說明
首先,對於任意一個viewController,iOS會以info.plist中的設定和當前viewController的preferredInterfaceOrientationForPresentation和supportedInterfaceOrientations三者支援的方法做一個交運算,若交集不為空白,則以preferredInterfaceOrientationForPresentation為初始方向,交集中的所有方向均支援,但僅在shouldAutorotate返回YES時,允許從初始方向旋轉至其他方向。若交集為空白,進入viewController時即crash,錯誤資訊中會提示交集為空白。
其次,UINavigationController稍有些特別,難以用常規API做到同一個naviVC中的ViewController在不同方向間自如地切換。(如果去SO之類的地方搜尋,會找到一個present empty viewController and then dismiss it之類的hacky trick,不太建議使用),如果要在橫豎屏間切換,建議使用presentXXX方法。
再次,AppDelegate中有一個委託方法可以動態設定應用支援的旋轉方向,且此委託的傳回值會覆蓋info.plist中的固定設定。使用該方法的便利之處不言自明,但缺點是搞明白當前哪個ViewController即將要被顯示,很可能會導致耦合增加;
最後,以上均為個人在iOS8 SDK下得到的實踐結果,請題主結合工程實際參考使用。
ios實現旋轉螢幕的方法