Swift: изменение ориентации с помощью PushViewConteroller - PullRequest
0 голосов
/ 21 января 2019

Я хочу изменить ориентацию вида, когда он перемещается из одного ViewController в другой контроллер вида. Например, когда он нажимает, он должен измениться на портрет в альбомной ориентации.

Я пытаюсь использовать этот код, но он не работает должным образом.

В ViewDidLoad

struct AppUtility {
        static func lockOrientation(_ orientation: UIInterfaceOrientationMask) {
            if let delegate = UIApplication.shared.delegate as? AppDelegate {
                delegate.orientationLock = orientation
            }
        }

        static func lockOrientation(_ orientation: UIInterfaceOrientationMask, andRotateTo rotateOrientation:UIInterfaceOrientation) {
            self.lockOrientation(orientation)
            UIDevice.current.setValue(rotateOrientation.rawValue, forKey: "orientation")
        }
    }

В ViewController

override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        AppDelegate.AppUtility.lockOrientation(UIInterfaceOrientationMask.landscapeRight, andRotateTo: UIInterfaceOrientation.landscapeRight)
    }

Но это не меняет своих ориентаций.

Ответы [ 2 ]

0 голосов
/ 21 января 2019

Я реализовал тот же код и получил ту же проблему, поэтому я добавил код ниже, прежде чем вызвать «lockOrientation» и решить его.

UIDevice.current.setValue(UIInterfaceOrientationMask.landscapeRight.rawValue, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
0 голосов
/ 21 января 2019

в приложении делегат

import UIKit
let appDelegate = UIApplication.shared.delegate as! AppDelegate
@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    var deviceOrientation = UIInterfaceOrientationMask.portrait
    func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
        return deviceOrientation
    }
}

В UIViewController

override func viewWillAppear(_ animated: Bool) {
        appDelegate.deviceOrientation = .landscapeLeft
        let value = UIInterfaceOrientation.landscapeLeft.rawValue
        UIDevice.current.setValue(value, forKey: "orientation")
    }
...