ios - 子层在 Swift 中调整为 UIView 边界
问题描述
我正在尝试将 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 元素:
有谁知道如何解决这个问题?
解决方案
你需要
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
cameraPreviewLayer?.frame = PreviewController.bounds
}
推荐阅读
- perl - 即使在对象被销毁后,状态变量仍然存在
- java - 如果函数有多个参数,如何在不使用 replace 方法的情况下替换 java 中的子字符串?
- gatsby - Gatsby MDX slugs 无法导航,名称中包含目录
- bash - curl: (3) URL 使用错误/非法格式或缺少 URL(但 curl 实际上执行 POST)
- bash - bash 脚本如何更改条件表达式以使其接受“y”和“Y”
- c++ - 如何在表格视图之间共享选择?
- python - 字典到 JSON 或 Dataframe 到 JSON 导出不产生方括号
- java - 用封闭的字符串和空格分割字符串
- node.js - Mongo:如何在级联关系中获取文档计数
- file - 每当它们在 github 中更新时,如何更新我的计算机中的文件?