iOS整体竖屏 个别页面横屏

个别页面设置横屏时出现了Bug,这里就我遇到的情况做个说明。

iOS竖屏状态下present一个横屏的viewController(继承BaseViewController)出现bug,每次app第一次启动后,会出现如附件中的现象,本应该横屏全屏的界面,结果成了竖屏只有上半边的情况,下半边全黑,再次进入这个页面就不会再出现。

图1.png

这时我在【General】【Device Orientation】中只选择了【Portrait】


图2.png

在需要横屏的ViewController中添加代码:

override var shouldAutorotate: Bool {
        return false
    }

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

解决办法

在【General】【Device Orientation】中选择了【Portrait】和【Landscape Right】,只在AppDelegate中设置

var allowRotation: Bool = false //是否允许转向

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        if self.allowRotation {
            return .landscapeRight
        } else {
            return .portrait
        }
    }

然后在需要设置横屏的Controller 的 viewDidLoad 函数中设置

if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            appDelegate.allowRotation = true
        }

在点击返回按钮时重新设置上面的值

if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            appDelegate.allowRotation = false
        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容