SwiftUI 1 つのビューを除くすべてのビューでポートレートを強制する 質問する

SwiftUI 1 つのビューを除くすべてのビューでポートレートを強制する 質問する

SwiftUI プロジェクトがあります。1 つを除くすべてのビューで、ポートレート モードのみを許可します。1 つのビューのみで、ポートレートとランドスケープの両方を許可します。Swift にはいくつかのリソースがありますが、SwiftUI には見つかりませんでした。

これを実現する方法を見つけた人はいますか?

ベストアンサー1

私もこれに似たようなことをしなければなりませんでした。これが私たちのアプローチです。

プロジェクトの向きをポートレートモードのみをサポートするように設定します。

次に、AppDelegate方向のインスタンス変数を追加し、supportedInterfaceOrientationsForデリゲート メソッドに準拠します。

static var orientationLock = UIInterfaceOrientationMask.portrait

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    return AppDelegate.orientationLock
}

次に、ランドスケープ ビューを表示するときに、次のアクションを実行します。

AppDelegate.orientationLock = UIInterfaceOrientationMask.landscapeLeft
UIDevice.current.setValue(UIInterfaceOrientation.landscapeLeft.rawValue, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()

そして解雇されると、

AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()

お役に立てれば!

おすすめ記事