轉自:http://blog.csdn.net/cococoolwhj/article/details/8208991
概述:
在iOS6之前的版本中,通常使用 shouldAutorotateToInterfaceOrientation 來單獨控制某個UIViewController的方向,需要哪個viewController支持旋轉,只需要重寫shouldAutorotateToInterfaceOrientation方法。
但是iOS 6裏屏幕旋轉改變了很多,之前的 shouldAutorotateToInterfaceOrientation 被列爲 DEPRECATED 方法,查看UIViewController.h文件也可以看到:
- // Applications should use supportedInterfaceOrientations and/or shouldAutorotate..
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation NS_DEPRECATED_IOS(2_0, 6_0);
程序將使用如下2個方法來代替:
- - (BOOL)shouldAutorotate;
- - (NSUInteger)supportedInterfaceOrientations;
1.
2.
另外要兼容IOS6之前的系統,要保留原來的 shouldAutorotateToInterfaceOrientation 方法,還有那些 willRotateToInterfaceOrientation 等方法。
IOS6自動旋轉設置:
- 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];
- }
- -(NSUInteger)supportedInterfaceOrientations{
- return UIInterfaceOrientationMaskAllButUpsideDown;
- }
- - (BOOL)shouldAutorotate{
- return YES;
- }
- UIViewController *viewCtrl2 = [[UIVewController alloc] init];
- [self.navigationController.navigationController pushViewController:viewCtrl2 animated:YES];
- - (BOOL)shouldAutorotate
- {
- return self.topViewController.shouldAutorotate;
- }
- - (NSUInteger)supportedInterfaceOrientations
- {
- return self.topViewController.supportedInterfaceOrientations;
- }
eg3:如果viewCtrl 是 presentModalViewController 到 viewCtrl3,那麼viewCtrl3的旋轉設置就不在navCtrl裏面了!如果presentModalViewController的viewController是navController、tabbarController包裝過的viewCtrl3,那麼就應在新包裝的navController、tabbarController裏面設置,如果是直接presentModalViewController到viewCtrl3,那麼就在viewCtrl3裏面設置
手動旋轉
手動旋轉也有2種方式,一種是直接設置 UIDevice 的 orientation,但是這種方式不推薦,上傳appStore有被拒的風險:
- if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
- [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIInterfaceOrientationPortrait];
- }
- self.view.transform = CGAffineTransformMakeRotation(M_PI/2)
下面講解採用第二種方式的各版本手動旋轉:
思想是首先設置 statusBarOrientation,然後再改變某個view的方向跟 statusBarOrientation 一致!
IOS6手動旋轉:
1. 那既然是旋轉,最少也得有2個方向,那麼還是少不了上面說的那個硬性條件,先在plist裏面設置好所有可能需要旋轉的方向。既然是手動旋轉,那麼就要關閉自動旋轉:
- - (BOOL)shouldAutorotate{
- return NO;
- }
- [[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的大小需要重新設置,不要使用self.view.frame,而是用bounds。
2. 如果shouldAutorotate 返回YES的話,下面設置setStatusBarOrientation 是不管用的!setStatusBarOrientation只有在shouldAutorotate 返回NO的情況下才管用!
IOS5、IOS4手動旋轉:
1.在需要手動旋轉的viewController裏的 shouldAutorotateToInterfaceOrientation 方法設置 interfaceOrientation == [UIApplicationsharedApplication].statusBarOrientation- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
- return (interfaceOrientation == [UIApplication sharedApplication].statusBarOrientation);
- }
- [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
- self.view.transform = CGAffineTransformMakeRotation(M_PI/2);
- self.view.bounds = CGRectMake(0, 0, kScreenHeight, 320);
經驗分享:
1.IOS6裏面,如果一個項目裏面需要各種旋轉支持,有自動,有手動,那麼我們可以新建2個navController或者tabbarController的子類,一個是不旋轉,一個旋轉,那麼所有需要旋轉的UINavigationController都可以用這個子類來代替!包括我們可以定製短信呀、郵件呀的旋轉!2.supportedInterfaceOrientations 方法一般是寫UIInterfaceOrientationMask方向,但是如果程序要兼容4.3以下的SDK(4.3以下的SDK必須是4.5以下的Xcode,不支持IOS6),那麼在用4.5以下的Xcode編譯的時候通不過!所以可以用statusBarOrientation代替或者直接寫死數字!
- -(NSUInteger)supportedInterfaceOrientations{
- return [UIApplication sharedApplication].statusBarOrientation;
- }
- UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
- UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
參考:
http://blog.csdn.net/totogogo/article/details/8002173
http://stackoverflow.com/questions/13200220/how-to-change-keyboard-orientation-in-ios6
http://blog.csdn.net/yiyaaixuexi/article/details/8035014