title: SubLayer overflowing root layer in iOS storyboards
body: <p>I have a sublayer (preview layer) which I programmatically set as the proof layer in Swift for a storyboard that overflows the root layer. I intend the video feed to stay inside the root view (red box) however, I can't set it.</p>
  <p><a href="https://i.stack.imgur.com/tE3Uw.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/tE3Uw.jpg" alt="Current view of the application" /></a></p>
  <p>Below is the code</p>
  <pre><code>        previewLayer = AVCaptureVideoPreviewLayer(session: session)
  //        previewLayer.videoGravity = AVLayerVideoGravity.resize // feed goes off the screen
  //        previewLayer.videoGravity = AVLayerVideoGravity.resizeAspect //feed goes off the screen at bottom left corner
          previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill// same result as resize
          
          
          if (previewLayer.connection!.isVideoMirroringSupported) {
              previewLayer.connection!.automaticallyAdjustsVideoMirroring = false
              previewLayer.connection!.isVideoMirrored = true
          }
          
          let exifOrientation = exifOrientationFromDeviceOrientation()
          
          rootLayer = self.layer
          previewLayer.setAffineTransform(CGAffineTransform(rotationAngle: degreeToRadian(90)).scaledBy(x: 1.0, y: -1.0))
          
          previewLayer.frame = rootLayer.bounds
          rootLayer.addSublayer(previewLayer)
  //        self.rootLayer.insertSublayer(previewLayer, at: 0)//yeild same problem
  </code></pre>
  <p>further when comparing the size of self and root layer they appear to be the same:</p>
  <pre><code>rootLayer.bounds = height = 1188.0 width = 607.0 self : height 1188.0 width = 607.0
  </code></pre>