iOS开发单个页面横屏处理(swift版)

问题

iOS开发中大部分情况都是只允许竖屏的,但是偶尔几个页面需要做横屏处理(或者只能横屏)

解决方案

Applegate.swift文件中的修改

首先在AppDelegate中新增属性

    var isLandscape = false

然后给AppDelegate新增扩展


// MARK: - 是否横屏
extension AppDelegate {
    @objc(application:supportedInterfaceOrientationsForWindow:) func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        if isLandscape {
            //return .landscapeRight //只允许全景模式(横屏)
            return .all //允许所有模式,手机转动会跟随转动
        }else{
            return .portrait //只能竖屏
        }
    }
}

/***
 解决iOS 9 横竖屏的问题
 This is a bug in iOS 9 that it failed to retrieve the supportedInterfaceOrientations for UIAlertController. And it seems it dropped to an infinite recursion loop in looking for the supportedInterfaceOrientations for UIAlertController
 https://stackoverflow.com/questions/31406820/uialertcontrollersupportedinterfaceorientations-was-invoked-recursively
 */
extension UIAlertController {
    open override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait
    }
    open override var shouldAutorotate: Bool {
        return false
    }
}

info.plist 添加

    <key>UIViewControllerBasedStatusBarAppearance</key>
    <true/>

设置好了后,接下来处理需要横屏幕的页面

需要横屏的页面BTKLineVC

// MARK: - 横屏处理
extension BTKLineVC {
    //运行页面随设备转动
    override var shouldAutorotate : Bool {
        return true
    }
    
    override func viewWillAppear(_ animated: Bool) {
        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            //允许横屏
            appDelegate.isLandscape = true
            
            // 强制横屏打开下面两行代码即可
            //let value = UIInterfaceOrientation.landscapeRight.rawValue
            //UIDevice.current.setValue(value, forKey: "orientation")
            
        }
        super.viewWillAppear(animated)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        navigationController?.setNavigationBarHidden(false, animated: false)
        
        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            //禁止横屏
            appDelegate.isLandscape = false
        }
        //强制为竖屏
        let value = UIInterfaceOrientation.portrait.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
        super.viewWillDisappear(animated)
    }
    
        //下面方法是处理navgation相关的逻辑,如果控制器没有nav,省略
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
        coordinator.animate(alongsideTransition: { [weak self] (context) in
            let orient = UIApplication.shared.statusBarOrientation
            switch orient {
            case .landscapeLeft, .landscapeRight:
                //横屏时禁止左拽滑出
                self?.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
                self?.navigationController?.setNavigationBarHidden(true, animated: false)
            default:
                //竖屏时允许左拽滑出
                self?.navigationController?.interactivePopGestureRecognizer?.isEnabled = true
                self?.navigationController?.setNavigationBarHidden(false, animated: false)
            }
        })
        super.viewWillTransition(to: size, with: coordinator)
    }
}

8月3号 补充

上面处理横屏会引起一个bug,就是如果手机横屏进入(与设置方向相同),布局可能会错乱。
原因是:这里布局我是按照横屏处理的,以上处理横屏方式适用于页面跟随系统,即页面可以横屏也可以竖屏

bug解决办法

  1. 设置系统只能竖屏


    image.png
  2. 需要横屏的页面取消方向跟随系统,只允许横屏

// MARK: - 横屏处理
extension BTKLineVC {
    //运行页面随设备转动
    override open var shouldAutorotate: Bool {
        return false
    }
    //支持的方向: 右边
    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .landscapeRight
    }
    // 优先展示的方向: 右边
    override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation {
        return .landscapeRight
    }
}

11月25号更新

在iOS13时,对于 present 的页面,需要设置取消弹框新模式 添加以下代码在viewDidLoad中即可:
modalPresentationStyle = UIModalPresentationStyle.fullScreen

备注

本文swift版本用的是swift4,如果代码异常根据版本稍作修改即可食用, 用OC的同学可以根据代码自行翻译,早用swift,脱离OC苦海。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,339评论 3 119
  • //去掉尾部空白 _tableView.tableFooterView = [[UIView alloc] in...
    软工官博阅读 1,156评论 0 1
  • 我偶尔会有想谈恋爱的心情。特别是在深夜。在心脏砰砰跳的时候。恰好我最近总是与凌晨三点的暮色相约。恋爱代表了太多美妙...
    Insurgentk_z阅读 182评论 0 0
  • 今天在上班路上看到一则显眼的显眼的新闻标题——《马云向马化腾和腾讯道歉,钉钉深夜致歉微信,昨天发生了什么?》,惊愕...
    肖较瘦阅读 513评论 2 1
  • 今天我们将再次进行昨天介绍过的冥想练习——打坐。请将昨天的说明再读一遍,然后按照指导练习,要牢记一点:只要你在练习...
    格锅阅读 126评论 0 0

友情链接更多精彩内容