iOS 让APP支持横竖屏

代码示例
  • Appdelegate添加属性(在Appdelegate.m中添加该属性)

    复制代码
      /** 是否允许横屏属性*/  
      @property (nonatomic,assign)BOOL isAllowRotation; 
  • 设置可以支持的方向(在AppDelegate.m中添加)

    复制代码
      - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window{
          if (self.isAllowRotation) {
              return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight;
          }
          return UIInterfaceOrientationMaskPortrait;
      }
  • 设置控制横竖屏打开关闭的方法(在AppDelegate.m中添加)

    复制代码
      /** 允许横屏*/  
      - (void)setCanAllowRotation{  
          self.isAllowRotation =YES;  
      }  
    
      /** 禁止横屏*/  
      - (void)setCanNotAllowRotation{  
          self.isAllowRotation =NO;  
      }  
  • 在AppDelegate.h中声明两个方法

    复制代码
      /** 支持横屏接口,需要在view消失时设置为不支持,否则其他页面也会支持横屏*/  
      - (void)setCanAllowRotation;  
      /** 不支持横屏接口*/  
      - (void)setCanNotAllowRotation;  
  • 在pch文件中添加这部分代码

    复制代码
      /**  获得appdelegate*/  
      #define APPDELEGATE (AppDelegate *)[[UIApplication sharedApplication] delegate]  
    
      //不支持横屏  
      #define CANNOTSCALE AppDelegate * appDelegate = APPDELEGATE;[appDelegate setCanNotAllowRotation];  
      //支持横屏  
      #define CANSCALE AppDelegate * appDelegate = APPDELEGATE;[appDelegate setCanAllowRotation];  
代码说明
  • 需要在需要使用的viewController中的viewWillAppear中打开支持横竖屏,然后在viewWillDisAppear中关闭支持横竖屏,下面是调用展示

    复制代码
      - (void)viewWillAppear:(BOOL)animated{
          [super viewWillAppear:animated];
          CANSCALE
      }
    
      - (void)viewWillDisappear:(BOOL)animated{
          [super viewWillDisappear:animated];
          CANNOTSCALE
      }
相关推荐
Benny的老巢1 天前
Mac上用XAMPP搭建局域网可访问的开发环境,让局域网内其他设备通过域名访问
nginx·macos·apache·xampp·php开发环境
2501_941881402 天前
在墨西哥城复杂流量环境下构建高稳定性API网关的架构设计与实现实践分享
macos·golang·xcode
wadesir2 天前
macOS Sequoia与macOS Tahoe全面对比:功能、性能与升级教程(新手入门指南)
macos
茅根竹蔗水__2 天前
iOS应用(App)生命周期、视图控制器(UIViewController)生命周期和视图(UIView)生命周期
ios
FreeBuf_2 天前
新型TCC绕过漏洞:macOS面临自动化攻击风险
macos·自动化·策略模式
Alice2 天前
Remote control Mac ios
macos
huaiyanchen2 天前
mac Navicat 下载及安装
macos
毛发浓密的女猴子2 天前
SSE Connect 数据解析详解
ios
人生何处不修行2 天前
iOS 自定义视频播放器实战:全屏旋转 + 画中画(PiP)+ 多页面切换不中断播放
macos·objective-c·cocoa