Iphone x 及以上的安全区域内的图像选择器相机覆盖不起作用

Image picker cameraoverlay within the safe area for Iphone x and above not working

我创建了自定义 UI 并将其分配给 UIImagePickerController 的相机叠加层,如下所示。

自定义视图

    cameraOverlay = RecordView.loadNib()
    cameraOverlay.frame = UIScreen.main.bounds
    cameraOverlay.buttonStartRecording.addTarget(self, action: #selector(buttonStartStopRecordingClicked),
                                                 for: .touchUpInside)
    cameraOverlay.buttonCancel.addTarget(self, action: #selector(buttonCancelClicked), for: .touchUpInside)
    cameraOverlay.buttonSwitchCamera.addTarget(self, action: #selector(buttonCameraSwitchClicked), for: .touchUpInside)
    
    cameraOverlay.buttonFlash.addTarget(self, action: #selector(buttonFlashClicked), for: .touchUpInside)
    cameraOverlay.labelTimer.text = "00:00/\(self.secondsToHoursMinutesSeconds(inputSeconds: recordingTimeLimit))"

图像选择器控制器

    imagePicker.delegate = self
    imagePicker.sourceType = .camera
    imagePicker.mediaTypes = [kUTTypeMovie as String]
    imagePicker.allowsEditing = true
    imagePicker.cameraOverlayView = cameraOverlay
    imagePicker.showsCameraControls = false
    imagePicker.cameraFlashMode = .off
    imagePicker.cameraCaptureMode = .video
    imagePicker.cameraDevice = .rear
    self.present(imagePicker, animated: true, completion: nil)

这适用于没有缺口的相机,但 UI 从 iPhone X 及以上的缺口后面的顶部被切掉。我不知何故需要在安全区域内设置我的自定义覆盖,但不确定如何完成。请有人能帮我解决这个问题。提前致谢。

我检查了 phone 是否有缺口

  var hasNotch: Bool {
    let bottom = UIApplication.shared.delegate?.window??.safeAreaInsets.bottom ?? 0
    return bottom > 0
}

然后据此处理约束

topHeightConstraint.constant = hasNotch ? 94 : 50
instructionBottomConstriant.constant = hasNotch ? 60 : 40