1、项目只设置支持横屏:
image.png
2、AppDelegate.h定义:
@property(nonatomic,assign)BOOL isRotationRight;//是否横屏
+ (AppDelegate *)appDelegate;
//设置横竖屏
- (void)setLandscapeRight:(BOOL)isRight;
3、AppDelegate.m实现
{
self.isRotationRight = isRight;//(以上2行代码,可以理解为打开横屏开关)
if (self.isRotationRight) {
NSNumber *resetOrientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationUnknown];
[[UIDevice currentDevice] setValue:resetOrientationTarget forKey:@"orientation"];
NSNumber *orientationTarget = [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight];
[[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"];
}
}
#pragma mark - 横竖屏管理
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window
{
if (self.isRotationRight == YES) {
return UIInterfaceOrientationMaskLandscapeRight;
}else{
return (UIInterfaceOrientationMaskPortrait);
}
}
4、哪个需要横屏调用:
[[AppDelegate appDelegate] setLandscapeRight:YES];
5、不需要横屏调用:
[[AppDelegate appDelegate] setLandscapeRight:NO];
Demo 地址
网友评论