横竖屏设置

指定页面横竖屏

920 发布: 2020/4/26 05:38 本文总阅读量

1.在AppDelegate.h文件中添加

/***  是否允许横屏的标记 */
@property (nonatomic,assign)BOOL allowRotation;

2.在AppDelegate.m中添加

-(UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    if (self.allowRotation) {//如果设置了allowRotation属性,支持全屏
       return UIInterfaceOrientationMaskAll;
}
    return UIInterfaceOrientationMaskPortrait;//默认全局不支持横屏
}

3.接着在置顶横竖屏UI界面设置

//进入全屏
-(void)begainFullScreen{
  AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  appDelegate.allowRotation = YES;
}
// 退出全屏
-(void)endFullScreen{
  AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  appDelegate.allowRotation = NO;
  //强制归正:
  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];
  }
}