進入正文前先認識幾個概念
portrait? 豎屏(Home鍵在下邊)
upside down? 豎屏(Home鍵在上邊)
landscape? 橫屏 ? |landscape left? 橫屏Home鍵在左邊
|landscape right 橫屏Home鍵在右邊
1、先讓窗口支持橫豎屏
兩種方法可以修改窗口對橫豎屏的支持
一種,代碼控制
在appDelegate中重寫方法,比如
- (UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window {
if(self.isShouAutoRotate) {
returnUIInterfaceOrientationMaskAll;
}
returnUIInterfaceOrientationMaskPortrait;
}
另一種就是,在【General】-->【Device Orientation】中設置好支持的方向
如圖

兩種方法的利弊,讀者自己推敲。
2、下面認識三個方法
//是否自動旋轉
- (BOOL)shouldAutorotate {
returnYES;
}
//返回支持的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
returnUIInterfaceOrientationMaskLandscapeRight;
}
//切換橫豎屏時,可以重寫這個方法,來重新布局界面
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {}
3、個別界面橫屏
第一種處理方式: ? 1中對應的設置支持橫屏完成后,在相應的控制器直接實現(xiàn)2中的方法二
第二種處理方式: ? 1中對應的設置支持橫屏完成后,在相應控制器的viewDidLoad方法中強制設置設備朝向,代碼如下:
NSNumber*value = [NSNumbernumberWithInt:4];
[[UIDevicecurrentDevice]setValue:valueforKey:@"orientation"];