MPMoviePlayerController 不在 UIView 的范围内

MPMoviePlayerController not staying within bounds of UIView

我正在尝试将视频添加到模态 window 中的 UIView 容器。当我添加视频时,它不会停留在它所绑定的 UIView 的范围内。这是它目前的样子的图片:

你可以看到右边缘的灰色容器是容器,这里是添加视频的视图控制器。

import Foundation
import MediaPlayer

class EvalInstructionsVC: UIViewController {
    private var moviePlayer : MPMoviePlayerController?

    @IBOutlet weak var video: UIView!

    func playVideo() {
        let path = NSBundle.mainBundle().pathForResource("Time to Rotate Demo", ofType:"mp4")
        let url = NSURL.fileURLWithPath(path!)
        moviePlayer = MPMoviePlayerController(contentURL: url)
        if let player = moviePlayer {
            player.view.frame = video.bounds
            player.view.center = CGPointMake(CGRectGetMidX(video.bounds), CGRectGetMidY(video.bounds))
            player.prepareToPlay()
            player.scalingMode = MPMovieScalingMode.AspectFill
            video.addSubview(player.view)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        playVideo()
    }

    @IBAction func done(sender: UIBarButtonItem) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }
}

如您所见,正在设置边界并将其作为子视图添加到视频 UIView,即上面屏幕截图中的灰色框。

如果有人能提供任何帮助,我将不胜感激。

干杯, 格里

稍后在视图控制器生命周期中调用 player.view.frame = video.bounds

最简单的解决方案是从 viewDidAppear:

调用 playVideo()
override func viewDidAppear(animated: Bool) {
    playVideo()
}

仅供参考,我们的代码非常相似(基于相同的示例?),我想您可能也不需要设置 player.view.center.