强制横屏

2018-12-10  本文已影响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];

上一篇下一篇

猜你喜欢

热点阅读