iOS16横屏

/// 开启横屏动作  允许左右横屏
    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横屏)