iOS8之后的屏幕旋轉(zhuǎn)和iOS6沧奴,7有很大不同奕污,項(xiàng)目中自己之前遇到過這樣的需求姓蜂,從A界面呈現(xiàn)B界面按厘,如果A橫屏則呈現(xiàn)出的B也為橫屏,如果A豎屏則呈現(xiàn)出的B也為豎屏钱慢,實(shí)現(xiàn)代碼如下:
//當(dāng)前屏幕高度
define MainScreenHeight [UIScreen mainScreen].bounds.size.height
//當(dāng)前屏幕寬度
define MainScreenWidth [UIScreen mainScreen].bounds.size.width
//webView是當(dāng)前滿屏的控件
- (void)viewDidLoad {
[super viewDidLoad]; [UIApplication sharedApplication].statusBarHidden = YES;
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait];
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if (UIInterfaceOrientationIsLandscape(orientation)) { if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0) { webViewWidth = MainScreenWidth; webViewHeight = MainScreenHeight; } else { webViewWidth = MainScreenHeight; webViewHeight = MainScreenWidth; } } else { webViewWidth = MainScreenWidth; webViewHeight = MainScreenHeight; }}
//隱藏狀態(tài)欄- (BOOL)prefersStatusBarHidden{ return YES;}//設(shè)置是否支持旋轉(zhuǎn)- (BOOL)shouldAutorotate { UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if (UIInterfaceOrientationIsPortrait(orientation)) { // 如果狀態(tài)欄豎著的逮京,不支持controller的旋轉(zhuǎn) return NO; } else if (UIInterfaceOrientationIsLandscape(orientation)) { return YES; } return NO;}// 直接返回支持的旋轉(zhuǎn)方向,該方法在iPad上的默認(rèn)返回值是UIInterfaceOrientationMaskAll束莫,iPhone上的默認(rèn)返回值是UIInterfaceOrientationMaskAllButUpsideDown- (NSUInteger)supportedInterfaceOrientations { UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if (UIInterfaceOrientationIsLandscape(orientation)) { return UIInterfaceOrientationMaskLandscape; } return UIInterfaceOrientationMaskPortrait;}
使用VC旋轉(zhuǎn)通過statusBarOrientation的方向判斷旋轉(zhuǎn)方向懒棉,在iOS8之前橫屏后,屏幕的寬和高的數(shù)值是對(duì)換了(即寬變成了高览绿,高變成了寬)策严,但在iOS8之后寬高并沒有對(duì)換,所以在iOS8之后旋轉(zhuǎn)橫屏如果不對(duì)換寬高值的話饿敲,就會(huì)造成屏幕一側(cè)為空的情況.(代碼是挺早之前寫的了妻导,可能會(huì)有些亂)上面的代碼是從A界面呈現(xiàn)B界面,如果A橫屏怀各,則present橫屏vc倔韭,豎屏下present豎屏vc。
如果僅在當(dāng)前vc進(jìn)行橫豎屏旋轉(zhuǎn)操作渠啤,可以監(jiān)聽系統(tǒng)的通知『UIWindowWillRotateNotification』狐肢,如下:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(screenRotate:animation:) name:@"UIWindowWillRotateNotification" object:nil];
- (void)screenRotate:(NSNotification *)noti animation:(BOOL)animation{UIInterfaceOrientation orientation = [[noti.userInfo objectForKey:@"UIWindowNewOrientationUserInfoKey"] integerValue]; if (!noti) { return; } animation = YES;NSTimeInterval i = [UIApplication sharedApplication].statusBarOrientationAnimationDuration; NSTimeInterval time = 0.3 + i; if (!animation) { time = 0.0; } switch (orientation) {case UIInterfaceOrientationPortrait: { } break;case UIInterfaceOrientationPortraitUpsideDown: { } break;case UIInterfaceOrientationLandscapeRight: {// 這里是給相應(yīng)的view對(duì)應(yīng)旋轉(zhuǎn) [UIView animateWithDuration:time animations:^{_userVc.view.transform = CGAffineTransformMakeRotation(M_PI_2); } completion:nil]; } break;case UIInterfaceOrientationLandscapeLeft: { // 這里是給相應(yīng)的的view對(duì)應(yīng)旋轉(zhuǎn) [UIView animateWithDuration:time animations:^{_userVc.view.transform = CGAffineTransformMakeRotation(-M_PI_2); } completion:nil]; } break; default: break; }}