强制横屏
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];