在 iOS 5.1 及更早版本中,我們通常透過 shouldAutorotateToInterfaceOrientation: 單獨控制某個 UIViewController 的旋屏方向:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
但在 iOS 6 中,該方法已被廢棄而失效。實踐後發現,僅覆寫 supportedInterfaceOrientations 並無法鎖定屏幕:
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
經多次實驗,總結出控制屏幕旋轉支持方向的可靠方法:子類化 UINavigationController,將旋屏判斷轉發給頂層的 view controller:
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
並將其設為程式入口,或指定為 self.window.rootViewController。若只想禁止某個 view controller 旋屏(全版本通用):
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(BOOL)shouldAutorotate
{
return NO;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
若想開啟某個 view controller 的全部方向支持:
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskAllButUpsideDown;
}
-(BOOL)shouldAutorotate
{
return YES;
}
如此便實現了對每個 view controller 的獨立控制。此外,若整個應用都不支持旋屏,可直接在 AppDelegate 中統一處理:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return UIInterfaceOrientationMaskPortrait;
}
首先需了解描述設備旋轉方向的四種狀態:

針對旋屏處理,大致有以下幾種思路:
1. 鎖定屏幕
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}
2. 支持旋屏或部分方向
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
3. 旋屏時直接改變控件大小與位置:系統只會拉伸背景圖,其他部件需自行處理,可在 willAnimateRotationToInterfaceOrientation 中調整:
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
[btn setFrame:CGRectMake(213, 442, 340, 46)];
}
if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
[btn setFrame:CGRectMake(280, 322, 460, 35)];
}
}
4. 讓控件透過旋轉自身來適應屏幕:透過監聽 UIDeviceOrientationDidChangeNotification 通知,並以 CGAffineTransformMakeRotation 對控件施加旋轉變換:
- (void)viewDidLoad
{
[super viewDidLoad];
UIDevice *device = [UIDevice currentDevice];
[device beginGeneratingDeviceOrientationNotifications];
NSNotificationCenter *ncenter = [NSNotificationCenter defaultCenter];
[ncenter addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:device];
}
-(void)orientationChanged
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
switch (orientation) {
case UIDeviceOrientationPortrait: [self rotation_btn:0.0]; break;
case UIDeviceOrientationPortraitUpsideDown: [self rotation_btn:180.0]; break;
case UIDeviceOrientationLandscapeLeft: [self rotation_btn:270.0]; break;
case UIDeviceOrientationLandscapeRight: [self rotation_btn:90.0]; break;
default: break;
}
}
此外,還可透過設置 autoresizesSubviews = YES 讓子視圖自動縮放,或為橫豎屏準備兩份不同的 Subview 布局並在切換時替換。記得調整 Storyboard/Xib 中的相關設定,以完成理想的自適應效果。
