在iOS開發(fā)的過程中,有時(shí)候會(huì)遇到固定的一個(gè)界面需要能隨意切換橫豎屏,而且其他的界面都不可以切換的時(shí)候。就必須在工程文件中如下圖,

這個(gè)時(shí)候的整個(gè)項(xiàng)目就都是處于可以橫豎屏切換的狀態(tài)。所以此時(shí),你必須通過在AppDelegate.m文件中用代碼來控制界面的橫切屏切換。
- (UIInterfaceOrientationMask)application:(UIApplication)application supportedInterfaceOrientationsForWindow:(UIWindow)window {
if(_isLandscape) {
//判斷當(dāng)前的界面橫豎屏狀態(tài)
UIDeviceOrientation orientation = [UIDevicecurrentDevice].orientation;
if(orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight){
return UIInterfaceOrientationMaskLandscape;
}else{//橫屏后旋轉(zhuǎn)屏幕變?yōu)樨Q屏
return UIInterfaceOrientationMaskPortrait;
}
}else{
return UIInterfaceOrientationMaskPortrait;
}
}
UIDeviceOrientation 是機(jī)器硬件的當(dāng)前旋轉(zhuǎn)方向 這個(gè)你只能取值 不能設(shè)置
這個(gè)時(shí)候,你就可以通過設(shè)置 _isLandscape的值來控制界面是否能進(jìn)行橫豎屏的切換。
typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
UIDeviceOrientationUnknown, //系統(tǒng)無法判斷目前Device的方向,有可能是斜置
UIDeviceOrientationPortrait, // 設(shè)備垂直方向上,按鈕在底部
UIDeviceOrientationPortraitUpsideDown, // 設(shè)備垂直方向上,按鈕在頂部
UIDeviceOrientationLandscapeLeft, // 設(shè)備水平方向,按鈕在右邊
UIDeviceOrientationLandscapeRight, // 設(shè)備水平方向,按鈕在左邊
UIDeviceOrientationFaceUp, // 面向設(shè)備的扁平化,屏幕向上
UIDeviceOrientationFaceDown // 面向設(shè)備的扁平化,屏幕向下
} __TVOS_PROHIBITED;
在ViewController里面,你可以通過獲取AppDelegate里面的_isLandscape值來控制屏幕能否橫豎屏切換
- (void)backToPortrait : (BOOL)isLan{//回到豎屏
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.isLandscape = isLan;
}

初次寫博客,可能表述上有些不清晰,需要的話可以留言,大家多討論。謝謝。