title: Swift: Resize an AVPlayerLayer to match the bounds of its parent container
body: <p>I have a local video playing using <code>AVPlayer</code> and <code>AVPlayerLayer</code> (Swift, iOS 8.2), and i need to resize the <code>AVPlayerLayer</code> (<code>introPlayerLayer</code>) when its parent view (<code>introView</code>) is resized, including on orientation change. So far nothing I have tried seems to work and the <code>playerLayer</code> has the wrong size and position.</p>
  
  <p>The <code>AVPlayer</code> is set up using a local mp4 video, and the <code>AVPlayerLayer</code> is initialized using that player. The layer is then added as a sublayer to the parent view.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>func playIntro() {
      let path = NSBundle.mainBundle().pathForResource("intro", ofType: "mp4")
      let url  = NSURL.fileURLWithPath(path!)
      let introPlayer           = AVPlayer(URL: url)
      introPlayer.allowsExternalPlayback = false
  
      var introPlayerLayer = AVPlayerLayer(player: introPlayer)
      introView.layer.addSublayer(introPlayerLayer)
      introPlayerLayer.frame = introView.bounds
  
      NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: introPlayer.currentItem)
  
      introView.hidden = false
      introPlayer.play()
  }
  </code></pre>
  
  <p>Should I be adding the <code>AVPlayerLayer</code> as a sublayer of the <code>UIView</code>, or should I be subclassing the parent container (<code>UIView</code>) as shown in the "The Player View" section of <a href="https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/02_Playback.html" rel="noreferrer">https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/02_Playback.html</a>. If so, how would I write that <code>PlayerView</code> subclass (shown in Objective-C) in Swift?
  Thank you in advance for any assistance.</p>