1. 程式人生 > >ios 強制橫屏大總結

ios 強制橫屏大總結

整個專案是豎屏的,不能橫屏,但是有個播放介面必須要橫屏於是就開始找各種橫屏的方法,最後在手機上好使了,但是在pad上橫屏啟動的時候介面是橫屏顯示,很是苦惱,就又開始了漫長的找資料,直接上程式碼

1,配置plist檔案和deployemnt Info -> device orientation

(1),plist檔案,如圖所示,第一項是建立專案時預設有的表示支援手機的螢幕方向(我把支援向右和向左的刪了),第二項是後加的表示支援ipad(新增 Supported interface orientations (iPad))的旋轉方向(同樣我把向左向右,向上的給刪了)

這裡寫圖片描述

(2),deployemnt Info -> device orientation,只選擇第一項豎屏。

這裡寫圖片描述

(1),AppDelegate.h裡面

@property (assign , nonatomic) BOOL isForceLandscape;
@property (assign , nonatomic) BOOL isForcePortrait;

AppDelegate.m裡面

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    if (self.isForceLandscape
) { return UIInterfaceOrientationMaskLandscape; }else if (self.isForcePortrait){ return UIInterfaceOrientationMaskPortrait; } return UIInterfaceOrientationMaskPortrait; }

(2),在相應的tabBarController裡面(我是在viewController裡面寫的)

#import <UIKit/UIKit.h>
#import "BaseViewController.h"
@interface RootViewController : BaseViewController @property (strong,nonatomic) UITabBarController *tabBarCon; @end
/// 選擇的當前控制器是否可以旋轉
-(BOOL)shouldAutorotate{

    return [self.tabBarCon.selectedViewController shouldAutorotate];
}

/// 選擇的當前控制器是支援的旋轉的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{

    return  [self.tabBarCon.selectedViewController supportedInterfaceOrientations];
}

///
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation{

    return  [self.tabBarCon.selectedViewController preferredInterfaceOrientationForPresentation];
}

(3),在navigationController裡面(自己建立的navigationController的子類)

#import <UIKit/UIKit.h>

@interface RootNavigationController : UINavigationController

//旋轉方向 預設豎屏
@property (nonatomic , assign) UIInterfaceOrientation interfaceOrientation;
@property (nonatomic , assign) UIInterfaceOrientationMask interfaceOrientationMask;
/// 當前的導航控制器是否可以旋轉
-(BOOL)shouldAutorotate{

    return YES;
}

//設定支援的螢幕旋轉方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {

    return self.interfaceOrientationMask;
}

//設定presentation方式展示的螢幕方向
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {

    return self.interfaceOrientation;
}

(4)在父類viewController裡面

- (void)viewDidLoad {
    [super viewDidLoad];

    // 表示本類支援旋轉
    [UIViewController attemptRotationToDeviceOrientation];
}

(5),在需要強制橫屏的地方呼叫

- (void)viewWillAppear:(BOOL)animated{

    self.navigationController.navigationBar.hidden = YES;
    self.navigationController.tabBarController.tabBar.hidden = YES;

    // 強制橫屏
    [self forceOrientationLandscape];

    RootNavigationController *nav = (RootNavigationController *)self.navigationController;
    nav.interfaceOrientation = UIInterfaceOrientationLandscapeRight;
    nav.interfaceOrientationMask = UIInterfaceOrientationMaskLandscapeRight;

    //強制翻轉螢幕,Home鍵在右邊。
    [[UIDevice currentDevice] setValue:@(UIInterfaceOrientationLandscapeRight) forKey:@"orientation"];
    //重新整理
    [UIViewController attemptRotationToDeviceOrientation];
}

- (void)viewWillDisappear:(BOOL)animated{

    //強制旋轉豎屏
    [self forceOrientationPortrait];
    RootNavigationController *navi = (RootNavigationController *)self.navigationController;
    navi.interfaceOrientation = UIInterfaceOrientationPortrait;
    navi.interfaceOrientationMask = UIInterfaceOrientationMaskPortrait;

    //設定螢幕的轉向為豎屏
    [[UIDevice currentDevice] setValue:@(UIDeviceOrientationPortrait) forKey:@"orientation"];
    //重新整理
    [UIViewController attemptRotationToDeviceOrientation];
}
#pragma  mark 橫屏設定
//強制橫屏
- (void)forceOrientationLandscape{

    AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    appdelegate.isForceLandscape=YES;
    appdelegate.isForcePortrait=NO;
    [appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
}

//強制豎屏
- (void)forceOrientationPortrait{

    AppDelegate *appdelegate=(AppDelegate *)[UIApplication sharedApplication].delegate;
    appdelegate.isForcePortrait=YES;
    appdelegate.isForceLandscape=NO;
    [appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
}