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:
cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
cameraPreviewLayer?.frame = PreviewController.bounds
PreviewController.layer.addSublayer(cameraPreviewLayer!)
When I tried to print the values of the frame and the bounds it gives me this:
print(PreviewController.bounds)
print(cameraPreviewLayer!.frame)
(0.0, 0.0, 375.0, 500.0)
(0.0, 0.0, 375.0, 500.0)
In the actual application, it results in the sublayer not filling up the whole UIView element:
Does anyone know how to resolve this?
You need to
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
cameraPreviewLayer?.frame = PreviewController.bounds
}