全屏AVPlayer无法旋转

时间:2017-03-14 18:19:14

标签: ios swift orientation avplayer avplayerviewcontroller

在我们的项目中,我们在此post之后阻止/取消阻止视图的方向并插入下面的代码。一切都很好。

AppDelegate.swift

func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
    if let rootViewController = self.topViewControllerWithRootViewController(window?.rootViewController) {
        if (rootViewController.respondsToSelector(Selector("canRotate"))) {
            // Unlock landscape view orientations for this view controller
            return .AllButUpsideDown;
        }
    }

    // Only allow portrait (standard behaviour)
    return .Portrait;
}

private func topViewControllerWithRootViewController(rootViewController: UIViewController!) -> UIViewController? {
    if (rootViewController == nil) { return nil }
    if (rootViewController.isKindOfClass(UITabBarController)) {
        return topViewControllerWithRootViewController((rootViewController as! UITabBarController).selectedViewController)
    } else if (rootViewController.isKindOfClass(UINavigationController)) {
        return topViewControllerWithRootViewController((rootViewController as! UINavigationController).visibleViewController)
    } else if (rootViewController.presentedViewController != nil) {
        return topViewControllerWithRootViewController(rootViewController.presentedViewController)
    }
    return rootViewController
}
...

ViewController.swift

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func viewWillDisappear(animated : Bool) {
        super.viewWillDisappear(animated)

        if (self.isMovingFromParentViewController()) {
            UIDevice.currentDevice().setValue(Int(UIInterfaceOrientation.Portrait.rawValue), forKey: "orientation")
        }
    }

    func canRotate() -> Void {}
}

除了我们启用了所有方向的视图外,该应用仅限纵向。在这个视图中还有一个带有AVPlayer的容器,问题是我不知道如何在这个AVPlayerViewController中启用所有方向。该应用程序允许视图中包含播放器的所有方向,但是一旦我将播放器置于全屏状态,这个播放器就不会旋转。我已尝试使用容器中AVPlayer的AVPlayerViewController中之前提到的代码,但播放器不会旋转。

0 个答案:

没有答案