【问题标题】:Sublayer resizing to UIView bounds in Swift子层在 Swift 中调整为 UIView 边界
【发布时间】:2019-07-30 21:07:37
【问题描述】:

我正在尝试将 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 layout sublayout


    【解决方案1】:

    你需要

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多