AVPlayerViewController 和 View 的约束

AVPlayerViewController and View's constraints

我正在使用 AVPlayerViewController 开发我的第一个应用程序。我需要使用此 class 内部视图显示视频流。在屏幕截图上,此视图具有深蓝色。在这个视图下,我有 Label 和 UICollectionView。当我点击项目时,会显示视频,但此视图的大小存在问题,大小已更改,视图下的标签变得不可见,视图顶部也有间隙。我使用 AutoLayout 设置约束并且它们是正确的,XCode 没有显示布局问题,但是在运行时点击项目以启动视频后,XCode 显示了很多约束错误。 这是布局的样子,没有错误或警告。

这是应用程序启动时的样子

当我点击 UICollectionView 的项目时,这是一个布局问题,正如您所看到的,带控制器的 AVPlayer 没有填充深蓝色视图(留下空闲 space),也改变了它的大小,所以标签是不可见的。此控制器的按钮也不起作用,当我点击视频时它会出现。

这是我为 AVPlayerViewController 设置框架的代码,myView 是深蓝色视图。

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        let url = NSURL(string:"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8")
        playerController.player = player
        self.addChildViewController(playerController)
        self.myView.addSubview(playerController.view)
        playerController.view.frame = self.myView.frame
        let playerItem = AVPlayerItem(URL: url!)
        player.replaceCurrentItemWithPlayerItem(playerItem)
        player.play()   
    }

这是执行此代码后输出中的错误:

Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it.

我在 Storyboard 上添加了 Container View、AVKit Player View Controller 而不是视图,并在它们之间建立了连接,还向 ViewController 添加了代码:

let child = childViewControllers[0] as! AVPlayerViewController
child.view.clipsToBounds = true
child.player = player

现在可以使用了