美文网首页ios积累
ios横屏或竖屏

ios横屏或竖屏

作者: 凤鹃一鸣 | 来源:发表于2017-07-31 16:19 被阅读29次

    框架 XMHVScreen

    在不同界面设置横屏或者竖屏,以及之间的切换

    #继承UINavigationController新建导航栏

    .m文件添加代码

    // 屏幕旋转

    -(BOOL)shouldAutorotate{

    return self.topViewController.shouldAutorotate;

    }

    //支持的方向

    - (UIInterfaceOrientationMask)supportedInterfaceOrientations {

    return self.topViewController.supportedInterfaceOrientations;

    }

    此导航栏设置为根控制器

    #导入头文件

    #import "XMNav.h"

    需要竖屏的控制器继承 XMVerticalScreenViewController

    需要横屏的控制器继承 XMHorizontalScreenViewController

    原理:

    设置竖屏控制器

    - (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    // 设置竖向方向

    [self setDirection];

    }

    // 设置竖向方向

    - (void)setDirection{

    NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];

    [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];

    NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationMaskPortrait];

    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

    }

    //支持的方向

    -(UIInterfaceOrientationMask)supportedInterfaceOrientations

    {

    return UIInterfaceOrientationMaskPortrait;

    }

    //是否可以旋转

    -(BOOL)shouldAutorotate

    {

    return YES;

    }

    设置横向方向

    - (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    // 设置横向方向

    [self setDirection];

    }

    // 设置竖横向方向

    - (void)setDirection{

    NSNumber *orientationUnknown = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];

    [[UIDevice currentDevice] setValue:orientationUnknown forKey:@"orientation"];

    NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];

    [[UIDevice currentDevice] setValue:orientationTarget forKey:@"orientation"];

    }

    //支持的方向

    -(UIInterfaceOrientationMask)supportedInterfaceOrientations

    {

    return UIInterfaceOrientationMaskLandscapeRight;

    }

    //是否可以旋转

    -(BOOL)shouldAutorotate

    {

    return YES;

    }

    gitHub下载demo地址

    关注微信公众号“应讯”

    关注微信公众号“应讯”

    相关文章

      网友评论

        本文标题:ios横屏或竖屏

        本文链接:https://www.haomeiwen.com/subject/zqsrlxtx.html