强制视图控制器方向在 iOS 16 beta 中工作吗

Abh*_*yam 23 iphone uiinterfaceorientation ios swift ios16

根据 iOS 和 iPadOS 16 Beta 3 发行说明:- 尝试在 \xc2\xa0 UIDevice \xc2\xa0via\xc2\xa0上设置方向setValue:forKey:\xc2\x80\x99 不受支持且不再有效。相反,他们说使用:preferredInterfaceOrientationForPresentation。

\n

preferredInterfaceOrientationForPresentation就我而言,通过使用或 ,强制视图控制器方向在 iOS 16 beta 中不起作用requestGeometryUpdate

\n

以前,UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue,\xc2\xa0forKey: "orientation")工作正常。

\n

小智 16

这个对我有用。

在应用程序委托中,

var orientation: UIInterfaceOrientationMask = .portrait
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return orientation
}
Run Code Online (Sandbox Code Playgroud)

在视图控制器中,

(UIApplication.shared.delegate as? AppDelegate)?.orientation = .landscapeRight
                    
let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene
windowScene?.requestGeometryUpdate(.iOS(interfaceOrientations: .landscapeRight))

UIApplication.navigationTopViewController()?.setNeedsUpdateOfSupportedInterfaceOrientations()
Run Code Online (Sandbox Code Playgroud)

在助手中,

extension UIApplication {
    class func navigationTopViewController() -> UIViewController? {
        let nav = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController
        return  nav?.topViewController
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @JY527 我怎样才能从 swiftUI 视图中得到它? (3认同)

小智 7

我的下面的代码的问题是,我试图在关闭模态视图时执行此操作,并且其下的视图更新得不够快。如果我将 requestGeometryUpdate 放在单独的按钮上,那么当我关闭视图时它就会起作用。

if #available(iOS 16.0, *) {

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

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

} 
Run Code Online (Sandbox Code Playgroud)


小智 1

我注意到我的问题似乎通过调用以下方法解决了:

[UIViewController setNeedsUpdateOfSupportedInterface

你可以尝试一下。