美文网首页
强制横屏

强制横屏

作者: 一笔春秋 | 来源:发表于2018-12-10 17:49 被阅读8次

    1、AppDelegate里设置属性

    @property(nonatomic,assign)BOOLallowLandscapeLeftRotation;//是否允许转向

    2、AppDelegate里设置方法

    - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullableUIWindow *)window

    {

        if(_allowLandscapeLeftRotation ==YES) {

            return UIInterfaceOrientationMaskLandscapeLeft;

        }else{

            return UIInterfaceOrientationMaskPortrait;

        }

    }

    - (void)setNewOrientation:(BOOL)isLandscapeLeft

    {

        if(isLandscapeLeft) {

            NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];

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

            NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft];

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

        }else{

            NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];

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

            NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationPortrait];

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

        }

    }

    3、调用

    AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;

    appDelegate.allowLandscapeLeftRotation = YES;

    [appDelegate setNewOrientation:YES];

    相关文章

      网友评论

          本文标题:强制横屏

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