I'm developing an iOS application (only portrait orientation) using swift. I've got a video section in which I present a list of videos. When a user taps on one of these, I use MPMoviePlayerViewController to present the movie player view.
Here my code:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let videoId = videos.objectAtIndex(indexPath.row).objectForKey("id") as! String
let videoPlayerViewController = XCDYouTubeVideoPlayerViewController.init(videoIdentifier: videoId)
videoPlayerViewController.moviePlayer.scalingMode = .AspectFit
videoPlayerViewController.moviePlayer.fullscreen = true
videoPlayerViewController.moviePlayer.controlStyle = .Fullscreen
videoPlayerViewController.moviePlayer.shouldAutoplay = true
videoPlayerViewController.moviePlayer.prepareToPlay
self.presentMoviePlayerViewControllerAnimated(videoPlayerViewController)
}
I enabled rotation on my AppDelegate as follows:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> UIInterfaceOrientationMask {
//If the video is being presented, let the user change orientation, otherwise don't.
if var presentedViewController = window?.rootViewController?.presentedViewController {
// Get the controller on the top of the stack
while (presentedViewController.presentedViewController) != nil {
presentedViewController = presentedViewController.presentedViewController!
}
if (presentedViewController.isKindOfClass(MPMoviePlayerViewController) && !presentedViewController.isBeingDismissed()) {
return UIInterfaceOrientationMask.All
}else{
return UIInterfaceOrientationMask.Portrait
}
}
return UIInterfaceOrientationMask.Portrait
}
Everything seems to work but when I come back from MPMoviePlayerViewController, my tableview is messed up as follows:
I think that this issue is due to AutoLayout, but I don't know how to fix it. Hope someone will help.
This is happening because when you open the video, even though your presenting view controller is portrait, it is getting rotated, and the layout is changed.
There are couple of solutions:
UIWindow
. This way when presenting the video, the presenting UIViewController
doesn't get changed at all.
iOS8 - prevent rotation on presenting viewController