總結(jié)iOS App開發(fā)中控制屏幕旋轉(zhuǎn)的幾種方式
在iOS6之前的版本中,通常使用 shouldAutorotateToInterfaceOrientation 來單獨控制某個UIViewController的方向,需要哪個viewController支持旋轉(zhuǎn),只需要重寫shouldAutorotateToInterfaceOrientation方法。
但是iOS 6里屏幕旋轉(zhuǎn)改變了很多,之前的 shouldAutorotateToInterfaceOrientation 被列為 DEPRECATED 方法,查看UIViewController.h文件也可以看到:
// Applications should use supportedInterfaceOrientations and/or shouldAutorotate..
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0);
程序?qū)⑹褂萌缦?個方法來代替:
- (BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations;
除了重寫這個2個方法,IOS6之后要旋轉(zhuǎn)還有一些需要注意的地方,下面會細(xì)述。另外還有一個硬性條件,需要在Info.plist文件里面添加程序支持的所有方向,可以通過以下2種方式添加
1.
2.
另外要兼容IOS6之前的系統(tǒng),要保留原來的 shouldAutorotateToInterfaceOrientation 方法,還有那些 willRotateToInterfaceOrientation 等方法。
自動旋轉(zhuǎn)設(shè)置:
控制某個viewController旋轉(zhuǎn)并不是像IOS5或者IOS4一樣在這個viewController里面重寫上面那2個方法,而是需要在這個viewController的rootViewController(根視圖控制器)里面重寫,怎么解釋呢?就是最前面的那個viewController,直接跟self.window接觸的那個controller,比如以下代碼:
UIViewController *viewCtrl = [[UIViewController alloc] init];
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:viewCtrl];
if ([window respondsToSelector:@selector(setRootViewController:)]) {
self.window.rootViewController = navCtrl;
} else {
[self.window addSubview:navCtrl.view];
}
如果需要設(shè)置viewCtrl的旋轉(zhuǎn),那么不能在UIViewController里面重寫shouldAutorotate和supportedInterfaceOrientations方法,而是需要在navCtrl里面設(shè)置,又因為UINavigationController是系統(tǒng)控件,所以這里需要新建一個UINavigationController的子navigationController的子類,然后在里面實現(xiàn)shouldAutorotate和supportedInterfaceOrientations方法,比如:
-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (BOOL)shouldAutorotate{
return YES;
}
eg1:如果上面的例子是self.window.rootViewController = viewCtrl,而不是navCtrl,那么上面的那2個控制旋轉(zhuǎn)的方法就應(yīng)該寫在UIViewController里面!
eg2:如果viewCtrl又pushViewController到viewCtrl2,需要設(shè)置viewCtrl2的旋轉(zhuǎn),怎么辦呢? 還是在navCtrl里面控制,因為viewCtrl和viewCtrl2的rootViewController都是navCtrl,一般的寫法都是
UIViewController *viewCtrl2 = [[UIVewController alloc] init];
[self.navigationController.navigationController pushViewController:viewCtrl2 animated:YES];
所以要控制一個UINavigationController push到的所有viewController的旋轉(zhuǎn),那么就得在navCtrl里面區(qū)分是哪個viewController,以便對他們一一控制!同樣如果rootViewController是UITabbarController,那么需要在子類化的UITabbarController里面重寫那2個方法,然后分別控制!
但是有時候我初始化UINavigationController的時候,并不知道所有我所有需要push到的viewController,那么這里有一個通用的方法,就是讓viewController自己來控制自己,首先在navCtrl里面的實現(xiàn)方法改為以下方式:
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
全部調(diào)用self.topViewController,就是返回當(dāng)前呈現(xiàn)出來的viewController里面的設(shè)置,然后在viewCtrl、viewCtrl2等等這些viewController里面重寫shouldAutorotate和supportedInterfaceOrientations,以方便設(shè)置每個viewController的旋轉(zhuǎn)
eg3:如果viewCtrl 是 presentModalViewController 到 viewCtrl3,那么viewCtrl3的旋轉(zhuǎn)設(shè)置就不在navCtrl里面了!如果presentModalViewController的viewController是navController、tabbarController包裝過的viewCtrl3,那么就應(yīng)在新包裝的navController、tabbarController里面設(shè)置,如果是直接presentModalViewController到viewCtrl3,那么就在viewCtrl3里面設(shè)置
手動旋轉(zhuǎn)
手動旋轉(zhuǎn)也有2種方式,一種是直接設(shè)置 UIDevice 的 orientation,但是這種方式不推薦,上傳appStore有被拒的風(fēng)險:
if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
[[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationPortrait];
}
第二種是假旋轉(zhuǎn),并沒有改變 UIDevice 的 orientation,而是改變某個view的 transform,利用 CGAffineTransformMakeRotation 來達(dá)到目的,比如:
self.view.transform = CGAffineTransformMakeRotation(M_PI/2)
下面講解采用第二種方式的各版本手動旋轉(zhuǎn):
思想是首先設(shè)置 statusBarOrientation,然后再改變某個view的方向跟 statusBarOrientation 一致!
那既然是旋轉(zhuǎn),最少也得有2個方向,那么還是少不了上面說的那個硬性條件,先在plist里面設(shè)置好所有可能需要旋轉(zhuǎn)的方向。既然是手動旋轉(zhuǎn),那么就要關(guān)閉自動旋轉(zhuǎn):
- (BOOL)shouldAutorotate{
return NO;
}
手動觸發(fā)某個按鈕,調(diào)用方法,這個方法的實現(xiàn)如下:
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
self.view.bounds = CGRectMake(0, 0, kScreenHeight, 320);
注意:
1. 只需要改變self.view.transform,那么self.view的所有subview都會跟著自動變;其次因為方向變了,所以self.view的大小需要重新設(shè)置,不要使用self.view.frame,而是用bounds。
2. 如果shouldAutorotate 返回YES的話,下面設(shè)置setStatusBarOrientation 是不管用的!setStatusBarOrientation只有在shouldAutorotate 返回NO的情況下才管用!
強(qiáng)制旋轉(zhuǎn)屏幕
最近接手了一個項目,正常情況下使用查看圖片是沒問題的。
用到了 MWPhotoBrowser 這個第三方圖片瀏覽庫。
不過發(fā)現(xiàn)了一個問題,就是設(shè)備橫屏modal這MWPhotoBrowser的時候,發(fā)生了圖片位置錯亂。
實在沒辦法,所以想到了一個餿主意。
就是modal的時候使用代碼把設(shè)備強(qiáng)制旋轉(zhuǎn)回去。
//UIDevice+WJ.h
@interface UIDevice (WJ)
/**
* 強(qiáng)制旋轉(zhuǎn)設(shè)備
* @param 旋轉(zhuǎn)方向
*/
+ (void)setOrientation:(UIInterfaceOrientation)orientation;
@end
//UIDevice+WJ.m
#import "UIDevice+WJ.h"
@implementation UIDevice (WJ)
//調(diào)用私有方法實現(xiàn)
+ (void)setOrientation:(UIInterfaceOrientation)orientation {
SEL selector = NSSelectorFromString(@"setOrientation:");
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[self instanceMethodSignatureForSelector:selector]];
[invocation setSelector:selector];
[invocation setTarget:[self currentDevice]];
int val = orientation;
[invocation setArgument:&val atIndex:2];
[invocation invoke];
}
@end
相關(guān)文章
IOS內(nèi)存泄漏檢查方法及重寫MLeakFinder
這篇文章主要介紹了IOS內(nèi)存泄漏檢查方法及如何重寫MLeakFinder,幫助ios開發(fā)者維護(hù)自身程序,感興趣的朋友可以了解下2021-04-04iOS實現(xiàn)設(shè)備判斷是否安裝相關(guān)地圖(百度、高德等)
這篇文章主要給大家介紹了關(guān)于iOS如何實現(xiàn)設(shè)備判斷是否安裝相關(guān)地圖,比如百度、高德等,其實實現(xiàn)的方法還是很簡單,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友下面來一起看看吧。2018-01-01iOS左右滑動標(biāo)簽頁導(dǎo)航的設(shè)計
這篇文章主要為大家詳細(xì)介紹了iOS左右滑動標(biāo)簽頁導(dǎo)航的設(shè)計思路,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06