Anton Anton - 1 year ago 259
iOS Question

AVPlayerViewController and View's constraints

I am developing my first app using AVPlayerViewController. I need to display video stream using this class inside view. On the screenshot this view has dark blue color. Under this view I have Label and UICollectionView. When I tap on item, video is displayed but there is a problem with size of this view, size is changed, label under view becomes not visible, also there is a gap on top of view. I use AutoLayout to set constrains and they are correct, XCode doesn't show problems with layout, but after tapping on item to start video during runtime, XCode shows a lot of errors with constraints.
This is how layout looks, no errors or warnings.


This is how app looks when it starts
app starts

This is a problem with layout when I tap on item of UICollectionView, as you can see AVPlayer with controller do not fill dark blue view (leaves free space), also changes its size, so label is not visible. Also buttons of this controller do not work, it appears when I tap on video.
problem with layout

This is a code where I set frame for AVPlayerViewController, myView is a dark blue view.

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

let url = NSURL(string:"")
playerController.player = player
playerController.view.frame = self.myView.frame
let playerItem = AVPlayerItem(URL: url!)

This is an error in the output after executing this code:

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.

Answer Source

Instead of a view I added Container View, AVKit Player View Controller on the Storyboard and made connection between them, also added a code to a ViewController:

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

now it works

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download