12.一键搞定iOS16横竖屏切换

前言

近期同事反馈视频横屏的功能失效了,在才发现 iOS16 下以前的方式无法使用了。于是完善了一些自己之前写的横竖屏切换的拓展。

一、 iOS16 之前

1.1 切换横屏

UIDevice.current.setValue(UIDeviceOrientation.landscapeLeft.rawValue, forKey: "orientation")

1.2 切换竖屏

UIDevice.current.setValue(UIDeviceOrientation.portrait.rawValue, forKey: "orientation")

1.3 切换结果监听

通过系统通知 UIDevice.orientationDidChangeNotification 监听提切换动作。

@objc
private func orientationDidChange(noti: Notification) {
    guard
        let device = noti.object as? UIDevice
    else {
        return
    }
    if device.orientation.isLandscape {
        print("横屏")
    } else {
        print("竖屏")
    }
}

二、 iOS16

更新了 iOS16 之后,发现上面的都没效果了,就...很可爱。iOS16 之后需要通过 Scene 来进行横竖屏的切换控制。

核心代码如下:

三、RyukieSwifty/FullScreen

iOS16

我对这些逻辑进行了封装可以简单的接入到项目之中。也提供了示例程序。GitHub: RyukieSwifty/FullScreen

pods 接入:

pod 'RyukieSwifty/FullScreen'

下面以一个 UIViewController 为例:

Last updated

Was this helpful?