我试图将AVCaptureVideoPreviewLayer作为UIView元素中的子层。我尝试使用以下方法将其框架设置为UIView边界:

cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
cameraPreviewLayer?.frame = PreviewController.bounds
PreviewController.layer.addSublayer(cameraPreviewLayer!)


当我尝试打印框架的值和边界时,它给出以下信息:

print(PreviewController.bounds)
print(cameraPreviewLayer!.frame)

(0.0, 0.0, 375.0, 500.0)
(0.0, 0.0, 375.0, 500.0)


在实际的应用程序中,它导致子层无法填充整个UIView元素:

ios - 在Swift中将子图层调整为UIView范围-LMLPHP

有谁知道如何解决这个问题?

最佳答案

你需要

override func viewDidLayoutSubviews() {
  super.viewDidLayoutSubviews()
  cameraPreviewLayer?.frame = PreviewController.bounds
}

07-26 09:40