iOS-强制横屏大总结

整个项目是竖屏的,不能横屏,但是有个播放界面必须要横屏于是就开始找各种横屏的方法,最后在手机上好使了,但是在pad上横屏启动的时候界面是横屏显示,很是苦恼,就又开始了漫长的找资料,直接上代码

1、配置plist文件和deployemnt Info -> device orientation

(1)plist文件,如图所示,第一项是建立项目时默认有的表示支持手机的屏幕方向(我把支持向右和向左的删了),第二项是后加的表示支持ipad(添加 Supported interface orientations (iPad))的旋转方向(同样我把向左向右,向上的给删了)

iOS-强制横屏大总结_第1张图片

(2)deployemnt Info -> device orientation,只选择第一项竖屏。

iOS-强制横屏大总结_第2张图片

2、开始代码配置(主要讲解push界面的强制横屏)主要是借鉴简书http://www.jianshu.com/p/5c773628caa6这位大神的讲解

(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 
#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 

@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];
}
  • 总结,感觉http://www.jianshu.com/p/5c773628caa6这位大神已经总结的很到位了,希望可以帮到需要的人

你可能感兴趣的:(iOS-OC)