美文网首页
iOS16横屏

iOS16横屏

作者: 大乐乐12 | 来源:发表于2022-08-22 18:06 被阅读0次
/// 开启横屏动作  允许左右横屏
    func landscape() {
        interfaceOrientationMask = .landscape

        if #available(iOS 16.0, *) {
            UIApplication.shared.keyWindow?.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations()
            let windowScene = UIApplication.shared.connectedScenes.first {$0 as? UIWindowScene != nil }
            if let windowScene = windowScene as? UIWindowScene {
                let geometryPreferences = UIWindowScene.GeometryPreferences.iOS(interfaceOrientations: .landscape)
                windowScene.requestGeometryUpdate(geometryPreferences)
            }
        }else {
            UIDevice.current.setValue(UIDeviceOrientation.portrait.rawValue, forKey: "orientation")
            UIDevice.current.setValue(UIDeviceOrientation.landscapeLeft.rawValue, forKey: "orientation")
        }
       
        
    }



/// 返回竖屏动作  仅允许竖直方向一个
    func portrait() {
        interfaceOrientationMask = .portrait
        
        if #available(iOS 16.0, *) {
            UIApplication.shared.keyWindow?.rootViewController?.setNeedsUpdateOfSupportedInterfaceOrientations()
            let windowScene = UIApplication.shared.connectedScenes.first {$0 as? UIWindowScene != nil }
            if let windowScene = windowScene as? UIWindowScene {
                let geometryPreferences = UIWindowScene.GeometryPreferences.iOS(interfaceOrientations: .portrait)
                windowScene.requestGeometryUpdate(geometryPreferences)
            }
        }else {
            UIDevice.current.setValue(UIDeviceOrientation.landscapeLeft.rawValue, forKey: "orientation")
            UIDevice.current.setValue(UIDeviceOrientation.portrait.rawValue, forKey: "orientation")
        }
    }
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return [DeviceOrientationManager shared].interfaceOrientationMask;
}

相关文章

网友评论

      本文标题:iOS16横屏

      本文链接:https://www.haomeiwen.com/subject/mlgigrtx.html