iOS 强制某个页面横屏遇到iPhone 6p横屏启动的坑

项目中有个页面需要强制横屏,代码逻辑实现如下。
首先,创建一个UINavigationController的子类CustomNavigationController,重载
shouldAutorotate,
supportedInterfaceOrientations,
preferredInterfaceOrientationForPresentation 三个方法

    /**
     *
     *  @return 是否支持旋转
     */
    override var shouldAutorotate : Bool {
        return viewControllers.last!.shouldAutorotate
    }
    /**
     *  适配旋转的类型
     *
     *  @return 类型
     */
    override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
        return viewControllers.last!.supportedInterfaceOrientations
    }

    override var preferredInterfaceOrientationForPresentation : UIInterfaceOrientation {
        return viewControllers.last!.preferredInterfaceOrientationForPresentation
    }

然后,在AppDelegate中设置rootViewController

    func setTheRootViewController() {
        let mainTabBarController = UIStoryboard(name: "Home", bundle: nil).instantiateViewController(withIdentifier: "mainTabBarController") as! MainTabBarController
        let navigationController = CustomNavigationViewController(rootViewController: mainTabBarController)
        navigationController.navigationBar.isTranslucent = false
        window?.rootViewController = navigationController
    }

之后,在基类BaseViewController里设置(假如没有基类,A 推到 B,在A里面写)

    override var shouldAutorotate : Bool {
        return false
    }
    
    override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.portrait
    }
    
    override var preferredInterfaceOrientationForPresentation : UIInterfaceOrientation {
        return UIInterfaceOrientation.portrait
    }

最后,在将要横屏的页面假如如下代码

    override var shouldAutorotate : Bool {
        return true
    }
    
    override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.landscapeRight
    }
    
    override var preferredInterfaceOrientationForPresentation : UIInterfaceOrientation {
        return UIInterfaceOrientation.landscapeRight
    }

但是,在测试过程中,发现iPhone 6s Plus 在桌面状态支持横屏了,那么就引起了横屏启动的问题。按上面代码实现之后,横屏启动时候界面是不转为横屏了,但是因为设备的 width 和 height 变了,所以整个界面的的width和height也跟着变了,但是方向没变,界面就乱了。

我的window.rootViewController 是 NavigationController, 然后在
AppDelegate的didFinishLaunchingWithOptions launchOptions:里这样写

application.statusBarOrientation = .portrait

CustomNavigationController里面也需要添加

UIApplication.shared.setStatusBarOrientation(.portrait, animated: false)

然后在需要横屏的页面里面

UIApplication.shared.setStatusBarOrientation(.landscapeRight, animated: false)

这样才实现了最终效果。而且进入横屏页面然后再推出原界面也不会出现横屏的问题。

因为一直都是用的swift开发,所以上面都是swift语法,OC只要自己转一下即可。

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

相关阅读更多精彩内容

  • 负能量爆棚的时候就会看部电影。 对电影没有特别的热爱,所以还有一大批好片等我去消灭。 大笑一场也好,泪流满面也罢,...
    荟荟陶阅读 241评论 0 1
  • 我们有四年的时间在一起 同一天开学 同一天毕业 虽然 我不认识你 送给一个好姑娘,大学校友,毕业后才知道。
    飘雨cc阅读 224评论 0 1
  • 今早是艳芳的关于这次培训的心得分享。今天分享得很好,虽然前面已经分享过五场了。可是今天艳芳的内容没有重复,从其他的...
    林玉珍阅读 251评论 4 3

友情链接更多精彩内容