iOS9之后强制横屏

1、IOS8之后有的方法写到类里强制横屏之后已经没有用了
- (BOOL)shouldAutorotate
{
    return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}
2、IOS8之后该怎么实现强制横屏

首先在代理类实现该方法:

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

然后在代理类头文件里定义2个全局变量

/** 横屏 */
@property (nonatomic, assign, getter=isForceLandscape) BOOL forceLandscape;
/** 竖屏 */
@property (nonatomic, assign, getter=isForcePortrait) BOOL forcePortrait;

最后一步,在你所需要实现强制横屏的ViewController里添加如下方法

/**
 *  强制横屏
 */
-(void)forceOrientationLandscape
{
    //这种方法,只能旋转屏幕不能达到强制横屏的效果
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = UIInterfaceOrientationMaskAllButUpsideDown;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
    //加上代理类里的方法,旋转屏幕可以达到强制横屏的效果
    lvAppDelegate *appdelegate=(lvAppDelegate *)[UIApplication sharedApplication].delegate;
    appdelegate.forceLandscape = YES;
    [appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
    
}
/**
 *  强制竖屏
 */
-(void)forceOrientationPortrait
{
    //加上代理类里的方法,旋转屏幕可以达到强制竖屏的效果
    lvAppDelegate *appdelegate=(lvAppDelegate *)[UIApplication sharedApplication].delegate;
    appdelegate.forcePortrait = YES;
    [appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self forceOrientationLandscape];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    //释放约束
    lvAppDelegate *appdelegate = (lvAppDelegate *)[UIApplication sharedApplication].delegate;
    appdelegate.forcePortrait = NO;
    appdelegate.forceLandscape = NO;
    [appdelegate application:[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
    //退出界面前恢复竖屏
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}

/**
 *  屏幕旋转时从新布局子控件
 */
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    [self.view layoutSubviews];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1、IOS8之后有的方法写到类里强制横屏之后已经没有用了 -(BOOL)shouldAutorotate{ ret...
    RmondJone阅读 1,157评论 0 0
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,252评论 4 61
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,217评论 30 472
  • 秋凉,冷雨又几场。 西北来的风,已吹过了山岗。 树叶渐黄,离人在归来的路上。 好花红过了春风,徜徉过深夏,飘荡在金...
    墨月霜花阅读 342评论 3 4
  • 微信小程序——连接一切的存在 有人说,得拥护者得天下。现在这句话可以转变为得微信拥有者得天下。由于被微信用户这个肥...
    A花火阅读 256评论 0 0