子层在 Swift 中调整到 UIView 边界
Sublayer resizing to UIView bounds in Swift
我正在尝试将 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)
在实际应用中,导致sublayer没有填满整个UIView元素:
有谁知道如何解决这个问题?
你需要
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
cameraPreviewLayer?.frame = PreviewController.bounds
}
我正在尝试将 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)
在实际应用中,导致sublayer没有填满整个UIView元素:
有谁知道如何解决这个问题?
你需要
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
cameraPreviewLayer?.frame = PreviewController.bounds
}