Iphone x 及更高版本的安全区域内的图像选择器相机覆盖不起作用

问题描述

我已经创建了一个自定义 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)

这适用于没有缺口的相机,但在 iPhone X 及更高版本上,UI 从缺口后面的顶部被切掉。我不知何故需要在安全区域内设置我的自定义叠加层,但不确定如何做到这一点。请有人可以帮助我。提前致谢。

解决方法

我检查了手机是否有缺口

  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