相信很多APP大多數(shù)頁面都是豎屏焕蹄,而只有部分頁面會(huì)旋轉(zhuǎn)纽绍,播放器是其代表之一。
但是工程的Landscape Left 和Landscape Right去掉,則部分頁面也不支持旋轉(zhuǎn)剩膘,Landscape Left 和Landscape Right開啟又導(dǎo)致默認(rèn)是的轉(zhuǎn)屏,所以我們需要自己來實(shí)現(xiàn)邏輯控制界面是否可轉(zhuǎn)并且還要默認(rèn)是豎屏
廢話也不多說盆顾,我來說說我的解決方案怠褐。
在設(shè)置的過程中首先提幾點(diǎn)要求:
1、默認(rèn)的所有頁面是豎屏的
2您宪、部分頁面可以通過設(shè)置開關(guān)支持旋轉(zhuǎn)惫搏。
解決思路:
1、首先我們先想到的是將plist的Landscape Left 和Landscape Right關(guān)掉蚕涤,但是關(guān)掉了工程的配置筐赔,在單個(gè)頁面開啟旋轉(zhuǎn)是行不通的。
2揖铜、那我們就不靠工程的配置茴丰,自己寫邏輯來控制,在iOS6之后AppDelegate里面有個(gè)
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
該方法在每次屏幕旋轉(zhuǎn)的時(shí)候可以設(shè)置頁面是否轉(zhuǎn)屏。
新建個(gè)工具類AutoRotate.h具體代碼邏輯如下
+ (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
if (window != nil) {
UIWindow *w = [UIApplication sharedApplication].delegate.window;
return [self orientationFromWindow:w];
}
return UIInterfaceOrientationMaskPortrait;
}
+ (UIInterfaceOrientationMask)orientationFromWindow:(UIWindow *)window {
UIViewController *rootViewController = window.rootViewController;
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabar = (UITabBarController *)rootViewController;
//獲取當(dāng)前選擇的navigationController
UINavigationController *nav = tabar.selectedViewController;
//獲取當(dāng)前顯示的viewController
UIViewController *topViewController = nav.topViewController;
//獲取當(dāng)前界面的presentedViewController
UIViewController *presentedViewController = topViewController.presentedViewController;
//正在顯示的viewController
UIViewController *showViewController = topViewController;
//如果有presentedViewController贿肩,則用
if (presentedViewController != nil) {
//TODO 這個(gè)判斷有待測試 下面的情況用的不多
if ([presentedViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController *nav = (UINavigationController *)presentedViewController;
UIViewController *topVC? = nav.topViewController;
showViewController = topVC;
} else {
showViewController = presentedViewController;
}
}
//默認(rèn)不轉(zhuǎn)屏
UIInterfaceOrientationMask orientation = UIInterfaceOrientationMaskPortrait;
BOOL shouldAutorotate = [self shouldAutorotate:showViewController];
//如果可轉(zhuǎn)屏峦椰,獲取轉(zhuǎn)屏方向
if (shouldAutorotate) {
orientation = [showViewController supportedInterfaceOrientations];
} else {
UIInterfaceOrientation statusBarOrientation = [UIApplication sharedApplication].statusBarOrientation;
switch (statusBarOrientation) {
case UIInterfaceOrientationLandscapeLeft:
orientation = UIInterfaceOrientationMaskLandscapeLeft;
break;
case UIInterfaceOrientationLandscapeRight:
orientation = UIInterfaceOrientationMaskLandscapeRight;
break;
default:
orientation = UIInterfaceOrientationMaskPortrait;
break;
}
}
return orientation;
}
return UIInterfaceOrientationMaskPortrait;
}
+ (BOOL)shouldAutorotate:(UIViewController *)showViewController {
//從方法列表中判斷是否實(shí)現(xiàn)了shouldAutorotate方法(因?yàn)閂iewController里面的實(shí)現(xiàn)不靠譜)
Class rotateClass = [showViewController class];
while (rotateClass != nil && rotateClass != [UIViewController class] && [rotateClass isSubclassOfClass:[UIViewController class]]) {
unsigned int methodCount;
Method *methods = class_copyMethodList(rotateClass, &methodCount);
for (unsigned int i = 0; i < methodCount; i++) {
SEL sel = method_getName(methods[i]);
if (sel == @selector(shouldAutorotate)) {//如果實(shí)現(xiàn)了方法汰规,判斷是否可轉(zhuǎn)屏
return [showViewController shouldAutorotate];
}
}
rotateClass = [rotateClass superclass];
}
return NO;
}
如果你的APP是tabbar布局可以直接用汤功,如果不是則需要更改rootViewController的轉(zhuǎn)換