How to force view controller orientation in iOS 8?

前端 未结 25 2099
太阳男子
太阳男子 2020-11-22 13:11

Before iOS 8, we used below code in conjunction with supportedInterfaceOrientations and shouldAutoRotate delegate methods to force app orie

25条回答
  •  长发绾君心
    2020-11-22 13:20

    My requirements are

    1. lock all views in portrait mode
    2. use AVPlayerViewController to play video

    When video is playing, if it's a landscape then allow the screen to rotate landscape right and landscape left. If it's a portrait then lock the view in portrait mode only.

    First, define supportedInterfaceOrientationsForWindow in AppDelegate.swift

    var portrait = true
    func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
        if portrait {
            return .Portrait
        } else {
            return .Landscape
        }
    }
    

    Second, in your main view controller, define following functions

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        print("\(#function)")
        return .Portrait
    }
    
    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
        return .Portrait
    }
    
    override func shouldAutorotate() -> Bool {
        return false
    }
    

    Then, you need to subclass AVPlayerViewController

    class MyPlayerViewController: AVPlayerViewController {
    
        var size: CGSize?
    
        var supportedOrientationMask: UIInterfaceOrientationMask?
        var preferredOrientation: UIInterfaceOrientation?
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            if let size = size {
                if size.width > size.height {
                    self.supportedOrientationMask =[.LandscapeLeft,.LandscapeRight]
                    self.preferredOrientation =.LandscapeRight
                } else {
                    self.supportedOrientationMask =.Portrait
                    self.preferredOrientation =.Portrait
                }
            }
        }
    

    Override these three functions in MyPlayerViewController.swift

    override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return self.supportedOrientationMask!
    }
    
    override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
        return self.preferredOrientation!
    }
    

    Because user might rotate device landscape left or landscape right, we need to set auto rotate to be true

    override func shouldAutorotate() -> Bool {
        return true
    }
    

    Finally, create MyPlayerViewController instance in your view controller and set the property size value.

    let playerViewController = MyPlayerViewController()
    
    // Get the thumbnail  
    let thumbnail = MyAlbumFileManager.sharedManager().getThumbnailFromMyVideoMedia(......)
    
    let size = thumbnail?.size
    playerViewController.size = size
    

    Initiate your player with proper videoUrl, then assign your player to playerViewController. Happy coding!

提交回复
热议问题