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

移动办公平台查看附件时支持横竖屏。而我们的app为了布局问题,没有支持横屏。我们就用下面的方法来使一个页面支持横竖屏。

在支持横竖屏的页面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
{
  //   NSLog(@"方向  =============   %ld", _allowRotate);
  if (_allowRotate == 1) {
    return UIInterfaceOrientationMaskAll;
  }else{
    return (UIInterfaceOrientationMaskPortrait);
  }
}
// 返回是否支持设备自动旋转
- (BOOL)shouldAutorotate
{
  if (_allowRotate == 1) {
    return YES;
  }
  return NO;
}

使其开放系统的横竖屏




知行办公,专业移动办公平台https://zx.naton.cn/
【总监】十二春秋之,[email protected]
【Master】zelo,[email protected]
【运营】狼行天下,[email protected];****
【产品设计】流浪猫,[email protected]
【体验设计】兜兜,[email protected]
【iOS】淘码小工,[email protected]iMcG33K,[email protected]
【Android】人猿居士,[email protected];思路的顿悟,[email protected]
【java】首席工程师MR_W,[email protected]
【测试】土镜问道,[email protected]
【数据】喜乐多,[email protected]
【安全】保密,你懂的。

你可能感兴趣的:(IOS 在app禁止横屏中使单独一个页面支持横竖屏)