美文网首页
IOS 在app禁止横屏中使单独一个页面支持横竖屏

IOS 在app禁止横屏中使单独一个页面支持横竖屏

作者: Edviin_2de8 | 来源:发表于2019-02-26 12:50 被阅读0次

在支持横竖屏的页面viewWillDisappear中使用一下代码:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
  
  AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  delegate.allowRotate = 1;
}

- (void)viewWillDisappear:(BOOL)animated
{
    AppDelegate * delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    delegate.allowRotate = 0;
    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];
  }
}

接着在appDelegate.h文件中声明

@property (nonatomic,assign)NSInteger allowRotate;
在appDelegate.m文件中添加一下方法:

//此方法会在设备横竖屏变化的时候系统会调用
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
  if (_allowRotate == 1) {
    return UIInterfaceOrientationMaskAll;
  }else{
    return (UIInterfaceOrientationMaskPortrait);
  }
}

相关文章

网友评论

      本文标题:IOS 在app禁止横屏中使单独一个页面支持横竖屏

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