IOS開啟旋轉(zhuǎn)

轉(zhuǎn)載:http://www.cnblogs.com/lear/p/5051818.html

1、屏蔽AppDelegate下面的屏幕旋轉(zhuǎn)方法#pragma mark - 屏幕旋轉(zhuǎn)的//- (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@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@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中重寫如下方法,就能實現(xiàn)豎屏,子ViewController再重寫實現(xiàn)旋轉(zhuǎn)

復制代碼

- (BOOL)shouldAutorotate{

//是否允許轉(zhuǎn)屏

return NO;

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations

{

//viewController所支持的全部旋轉(zhuǎn)方向

return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;

}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

{

//viewController初始顯示的方向

return UIInterfaceOrientationPortrait;

}

復制代碼

注意:在使用的時候發(fā)現(xiàn)在ios9以下的版本出現(xiàn)一個奇怪的問題,就是編譯出來的app默認是橫的,解決辦法是看app.plist下面Supported interface orientations里的列表中,正屏的是不是排在第一個。

摘自網(wǎng)上網(wǎng)友的回復內(nèi)容,如下:

以下方法僅對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];

}

在真正實現(xiàn)界面的ViewController里,override上面這三個方法,override規(guī)則如下:

preferredInterfaceOrientationForPresentation表示viewController初始顯示時的方向;

supportedInterfaceOrientations是在該viewController中支持的所有方向;

shouldAutorotate表示是否允許旋屏。

流程說明

首先,對于任意一個viewController,iOS會以info.plist中的設置和當前viewController的preferredInterfaceOrientationForPresentation和supportedInterfaceOrientations三者支持的方法做一個交運算,若交集不為空,則以preferredInterfaceOrientationForPresentation為初始方向,交集中的所有方向均支持,但僅在shouldAutorotate返回YES時,允許從初始方向旋轉(zhuǎn)至其他方向。若交集為空,進入viewController時即crash,錯誤信息中會提示交集為空。

其次,UINavigationController稍有些特別,難以用常規(guī)API做到同一個naviVC中的ViewController在不同方向間自如地切換。(如果去SO之類的地方搜索,會找到一個present empty viewController and then dismiss it之類的hacky trick,不太建議使用),如果要在橫豎屏間切換,建議使用presentXXX方法。

再次,AppDelegate中有一個委托方法可以動態(tài)的設置應用支持的旋轉(zhuǎn)方向,且此委托的返回值會覆蓋info.plist中的固定設置。使用該方法的便利之處不言自明,但缺點是搞明白當前哪個ViewController即將要被顯示,很可能會導致耦合增加;

最后,以上均為個人在iOS8 SDK下得到的實踐結(jié)果,請題主結(jié)合工程實際參考使用。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容