title: Sublayer resizing to UIView bounds in Swift
body: <p>I'm trying to put an AVCaptureVideoPreviewLayer as a sublayer in a UIView element. I tried to set its frame to the UIView bounds by using:</p>
  
  <pre><code>cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
  cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
  cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
  cameraPreviewLayer?.frame = PreviewController.bounds
  PreviewController.layer.addSublayer(cameraPreviewLayer!)
  </code></pre>
  
  <p>When I tried to print the values of the frame and the bounds it gives me this:</p>
  
  <pre><code>print(PreviewController.bounds)
  print(cameraPreviewLayer!.frame)
  
  (0.0, 0.0, 375.0, 500.0)
  (0.0, 0.0, 375.0, 500.0)
  </code></pre>
  
  <p>In the actual application, it results in the sublayer not filling up the whole UIView element:</p>
  
  <p><a href="https://i.stack.imgur.com/yr4oI.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yr4oI.jpg" alt="Error"></a></p>
  
  <p>Does anyone know how to resolve this?</p>