第一步
首先保證工程支持橫豎屏 不多說看圖
保證圈紅的地方 打對勾
58F678EC-EABC-4320-9FCB-F72BA617AFB5.png
第二步:
分兩種情況
第一種:
你的window的rootViewController是一個UITabBarController
這時候你就需要創建一個 繼承自 UITabBarController 的YourProjectTabBarController
然后在你的YourProjectTabBarController.m里添加如下的三個方法
OC實現
//是否跟隨屏幕旋轉
-(BOOL)shouldAutorotate{
return self.selectedViewController.shouldAutorotate;
}
//支持旋轉的方向有哪些
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return [self.selectedViewController supportedInterfaceOrientations];
}
//控制 vc present進來的橫豎屏和進入方向 ,支持的旋轉方向必須包含改返回值的方向 (詳細的說明見下文)
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return [self.selectedViewController preferredInterfaceOrientationForPresentation];
}
Swift實現
//是否跟隨屏幕旋轉
override func shouldAutorotate() -> Bool {
return (selectedViewController?.shouldAutorotate())!
}
//支持旋轉的方向有哪些
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return (selectedViewController?.supportedInterfaceOrientations())!
}
//控制 vc present進來的橫豎屏和進入方向 ,支持的旋轉方向必須包含改返回值的方向 (詳細的說明見下文)
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return (selectedViewController?.preferredInterfaceOrientationForPresentation())!
}
如果YourProjectTabBarController 里裝載的視圖 又是這種結構的
OC舉例
UIViewController * vc1 = [[UIViewController alloc]init];
BaseNavigationViewController * nvc1 = [[BaseNavigationViewController alloc]initWithRootViewController:vc1];
UIViewController * vc2 = [[UIViewController alloc]init];
BaseNavigationViewController * nvc2 = [[BaseNavigationViewController alloc]initWithRootViewController:vc2];
UIViewController * vc3 = [[UIViewController alloc]init];
BaseNavigationViewController * nvc3 = [[BaseNavigationViewController alloc]initWithRootViewController:vc3];
self.viewControllers = @[nvc1,nvc2,nvc3];
Swift舉例
let vc1? = UIViewController()
let nvc1 = BaseNavigationViewController(rootViewController: vc1)
let vc2? = UIViewController()
let nvc2 = BaseNavigationViewController(rootViewController: vc2)
let vc3? = UIViewController()
let nvc3 = BaseNavigationViewController(rootViewController: vc3)
viewControllers = [nvc1, nvc2, nvc3]
即 tabbarController的viewControllers里放的是導航欄視圖而不是不同的UIViewController
這時候你需要創建一個繼承自UINavigationController 的YourProjectNavigationController
然后在你的YourProjectNavigationController.m里添加如下的三個方法
OC實現
-(BOOL)shouldAutorotate{
return self.topViewController.shouldAutorotate;
}
-(UIInterfaceOrientationMask)supportedInterfaceOrientations{
return [self.topViewController supportedInterfaceOrientations];
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
Swift實現
override func shouldAutorotate() -> Bool {
return (topViewController?.shouldAutorotate())!
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return (topViewController?.supportedInterfaceOrientations())!
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return (topViewController?.preferredInterfaceOrientationForPresentation())!
}
仔細看和上面YourProjectTabBarController里的還是有區別的。
一個是? self.selectedViewController 一個是 self.topViewController
接下來就可以控制每一個Controller的是否可以旋轉了(前提 “iphone的豎排方向鎖定” 是關閉的狀態)
在需要旋轉的controller里添加如下方法
OC實現
//是否旋轉
-(BOOL)shouldAutorotate{
return YES;
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
Swift實現
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.All
}
在不需要旋轉的controller里添加如下方法
OC實現
//是否旋轉
-(BOOL)shouldAutorotate{
return NO;
}
//支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
Swift實現
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
tips:有一個小建議 你可以創建一個繼承自UIViewController的BaseViewController,然后你項目里創建的vc都繼承自BaseViewController。你可以在BaseViewController 添加屏幕不能旋轉的方法 ,這樣創建的每一個vc都不能旋轉。 然后,如果你需要旋轉的時候,在vc里添加支持旋轉的代碼就好了。這樣就省去了,如果大多數vc不需要支持屏幕旋轉,重復添加不支持旋轉的代碼了。
第2種情況:
你的window的rootViewController是一個UINavigationController
這種情況 你只要省去上面創建 繼承自 UITabBarController 的YourProjectTabBarController的步驟就可以了。
詳細說下這個方法
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationPortrait;
}
共有5個返回值
//該方法 只適用于 vc present 進入? 不適用于 push
typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
UIInterfaceOrientationUnknown? ? ? ? ? ? //貌似沒什么用
UIInterfaceOrientationPortrait? ? ? ? ? //vc 豎屏 從下? present 進入
UIInterfaceOrientationPortraitUpsideDown //vc 豎屏 從上? present 進入
UIInterfaceOrientationLandscapeLeft? ? ? //vc 橫屏 從左? present 進入
UIInterfaceOrientationLandscapeRight? ? //vc 橫屏 從又? present 進入
} __TVOS_PROHIBITED;
//這個方法需要和下面的方法一起使用
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
//支持的旋轉方向必須包含改返回值的方向 這句話的意思是:
舉幾個例子
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait; // 支持豎屏
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeLeft; //卻橫屏進入
}
這樣的組合會造成程序崩潰
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll; // 支持所有狀態
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeLeft; //橫屏進入
}
或
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape; // 支持橫屏狀態
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{
return UIInterfaceOrientationLandscapeLeft; //橫屏進入
}
這兩種就不會崩潰。
該方法主要用于,你當前處于豎屏狀態但是你希望下一個頁面是 橫屏狀態 的時候。且 “iphone的豎排方向鎖定” 是否開啟 不會影響該方法。
方法三:
除了上面的橫豎屏方法還有另一個,但是部分跟上面2個方法的設置是一樣的,比如方向設定.
// 強制轉屏
- (void)setInterfaceOrientation:(UIInterfaceOrientation)orientation {
if([[UIDevicecurrentDevice]respondsToSelector:@selector(setOrientation:)]) {
SELselector=NSSelectorFromString(@"setOrientation:");
NSInvocation*invocation = [NSInvocationinvocationWithMethodSignature:[UIDeviceinstanceMethodSignatureForSelector:selector]];
[invocationsetSelector:selector];
[invocationsetTarget:[UIDevicecurrentDevice]];
// 從2開始是因為前兩個參數已經被selector和target占用
[invocationsetArgument:&orientationatIndex:2];
[invocationinvoke];
}
}
方法四:重置一下rootViewController,如此系統才會重新觸發- (UIInterfaceOrientationMask)supportedInterfaceOrientations {}等方法.
// hack, turn to landscape code.
UIWindow*mainWindow = [[UIApplicationsharedApplication].delegatewindow];
UIViewController*root = mainWindow.rootViewController;
mainWindow.rootViewController=nil;
mainWindow.rootViewController= root;
if([rootisKindOfClass:[UITabBarControllerclass]]) {
UITabBarController*tab = (UITabBarController*)root;
NSIntegercurrentIdx = tab.selectedIndex;
[tabsetSelectedIndex:(currentIdx+1)%4];
[tabsetSelectedIndex:currentIdx];
}
總結:
對于方法三、四注意事項:應用在橫屏進入后臺時,要預防方向被修改為豎屏。同時要注意在AppDelegate中設置要某些類需要的方向。此文第一步、第二部為轉載的下面作者的內容,有問題的,歡迎討論:QQ:1401699567.
這篇文章作為基礎知識還是很有參考價值的:http://www.lxweimin.com/p/62431e148e68
轉載聲明:
作者:張梓辰
鏈接:http://www.lxweimin.com/p/a2201f39b6a7
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。