iOS 16 swift 横屏适配

手动横屏方法:

if #available(iOS 16.0, *) {

                let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene

                windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: .landscapeRight))

            } else {

                // Fallback on earlier versions

                UIDevice.switchNewOrientation(.landscapeRight)

            }

手动竖屏方法:

        if #available(iOS 16.0, *) {

            let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene

            windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: .portrait))

        } else {

            // Fallback on earlier versions

            UIDevice.switchNewOrientation(.portrait)

        }

获取当前屏幕横竖屏状态:

        if #available(iOS 16.0, *) {

            let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene

            if (windowScene?.interfaceOrientation.isLandscape)! as Bool {

                //横屏

            }else {

                //竖屏

            }

        }

如果手动用代码强制横屏,与自动旋转手机屏幕横屏,两者有不生效的情况,可以执行以下代码,更新横竖屏状态。

        if #available(iOS 16.0, *) {

            self.setNeedsUpdateOfSupportedInterfaceOrientations()

        }

你可能感兴趣的:(iOS开发,ios,swift)