橫豎屏切換幾乎每個 iOS 項目都會遇到。看似簡單,真正動手時卻有不少細節和坑。本文把常見的幾種情形與實現方式做個系統總結。
橫屏的兩個方向其實是相反的:UIInterfaceOrientationLandscapeRight 與 UIInterfaceOrientationMaskLandscapeRight 都代表 Home 鍵在右側的橫屏;而 UIDeviceOrientationLandscapeLeft 則是 Home 鍵在左側。
只需在【General】→【Device Orientation】中勾選希望支持的方向即可。設置完成後,豎屏時所有界面豎屏顯示,橫屏(Home 在右側)時所有界面橫屏顯示,其他方向則不旋轉。
這裡有個坑:iOS 9 之後橫屏時狀態欄會消失。解法是確保 plist 中【View controller-based status bar appearance】為 YES,並重寫:
- (BOOL)prefersStatusBarHidden { return NO; }
在【Device Orientation】設置好支持方向後,只需在特殊視圖控制器中重寫兩個方法:
- (BOOL)shouldAutorotate { return YES; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeRight; }
這是最常見的需求,比如視頻播放類 App。有兩種處理方式。
方式一(借助基類)
在【Device Orientation】中設置好所有需要支持的方向,然後用一個基類控制器,在其中重寫:
- (BOOL)shouldAutorotate { return YES; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskPortrait; }
特殊界面再重寫這兩個方法,允許自動切換:
- (BOOL)prefersStatusBarHidden { return NO; } - (BOOL)shouldAutorotate { return YES; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortrait; }
方式二(借助通知)
如果不想每個控制器都繼承基類,可以借助設備方向通知來控制。先在【Device Orientation】中設置僅支持豎屏,然後在特殊視圖控制器的 viewDidLoad 中註冊通知:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil]; - (void)deviceOrientationDidChange { if([UIDevice currentDevice].orientation == UIDeviceOrientationPortrait) { [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait]; [self orientationChange:NO]; } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) { [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight]; [self orientationChange:YES]; } } - (void)orientationChange:(BOOL)landscapeRight { if (landscapeRight) { [UIView animateWithDuration:0.2f animations:^{ self.view.transform = CGAffineTransformMakeRotation(M_PI_2); self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); }]; } else { [UIView animateWithDuration:0.2f animations:^{ self.view.transform = CGAffineTransformMakeRotation(0); self.view.bounds = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); }]; } } #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width) #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
最重要的一點:需要重寫並返回 NO:
- (BOOL)shouldAutorotate { return NO; }
這樣設備橫屏時界面變橫屏,豎屏時變豎屏。
方式二因為只設置了豎屏,橫屏彈出鍵盤時會是豎屏樣式,解法是在【Device Orientation】中補上橫屏方向。
如果 ViewController 放在 UINavigationController 或 UITabBarController 中,需要轉發方向控制方法:
// UINavigationController - (BOOL)shouldAutorotate { return [self.topViewController shouldAutorotate]; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return [self.topViewController supportedInterfaceOrientations]; } // UITabBarController - (BOOL)shouldAutorotate { return [self.selectedViewController shouldAutorotate]; } - (UIInterfaceOrientationMask)supportedInterfaceOrientations { return [self.selectedViewController supportedInterfaceOrientations]; }
如果想點擊按鈕後強制旋轉,直接調用 setStatusBarOrientation: 會導致返回時方向錯亂。一種更可靠的寫法是在開源項目中常見的做法——通過 NSInvocation 調用私有方法強制設置設備方向,再配合方向通知完成切換:
- (IBAction)landscapAction:(id)sender { [self interfaceOrientation:UIInterfaceOrientationLandscapeRight]; } - (void)interfaceOrientation:(UIInterfaceOrientation)orientation { if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) { SEL selector = NSSelectorFromString(@"setOrientation:"); NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]]; [invocation setSelector:selector]; [invocation setTarget:[UIDevice currentDevice]]; int val = orientation; [invocation setArgument:&val atIndex:2]; [invocation invoke]; } }