将UIImagePickerController CameraOverlayView放置在相机预览的顶部

Positioning a UIImagePickerController cameraOverlayView on top of the camera preview(将UIImagePickerController CameraOverlayView放置在相机预览的顶部)
本文介绍了将UIImagePickerController CameraOverlayView放置在相机预览的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向我的UIImagePickerController添加一个覆盖,但我只需要它来覆盖相机预览,而不是顶部或底部的相机控件。我在任何地方都找不到这样做的方法,因为每个设备上的顶部/底部控件高度都不同。我以前在其他应用程序上见过这样的做法。如有任何指导,我们不胜感激。

imagePicker.sourceType = .camera
            
let overlay = UIView()
overlay.backgroundColor = UIColor(white: 1, alpha: 0.5)

imagePicker.cameraOverlayView!.addSubview(overlay)
// This is using a UIView extension to constrain my views. It's as simple as it looks.
overlay.anchor(top: imagePicker.cameraOverlayView!.topAnchor, left: imagePicker.cameraOverlayView!.leftAnchor, bottom: imagePicker.cameraOverlayView!.bottomAnchor, right: imagePicker.cameraOverlayView!.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 100, height: 100)
        
self.present(imagePicker, animated: true, completion: nil)

我希望此半透明的UIView仅填充红色区域。我创建3:4的框架没有问题,但它不会正确地定位在Y轴上。imagePicker.cameraOverlayView!.centerYAnchor与设备的centerYAnchor相同。

推荐答案

可能不是最佳解决方案。使用视图调试器,可以检查视图层次结构。现在将该视图添加到子级camera view port controller

imagePicker = CustomPicker()
imagePicker.sourceType = .camera
present(imagePicker, animated: true, completion: nil)


class CustomPicker: UIImagePickerController {
    let overlay = UIView()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if let camController = children.first?.children.first?.children.first, overlay.superview == nil {
            overlay.backgroundColor = UIColor(white: 1, alpha: 0.5)
            camController.view.addSubview(overlay)
            overlay.addPinConstraints(top: 0, left: 0, bottom: 0, right: 0)
        }
    }
}

extension UIView {
    func addPinConstraints(top: CGFloat? = nil, left: CGFloat? = nil, bottom: CGFloat? = nil, right: CGFloat? = nil) {
        guard let parent = superview else { return }
        translatesAutoresizingMaskIntoConstraints = false
        if let left = left {
            leadingAnchor.constraint(equalTo: parent.leadingAnchor, constant: left).isActive = true
        }
        if let right = right {
            trailingAnchor.constraint(equalTo: parent.trailingAnchor, constant: -right).isActive = true
        }
        if let top = top {
            topAnchor.constraint(equalTo: parent.topAnchor, constant: top).isActive = true
        }
        if let bottom = bottom {
            bottomAnchor.constraint(equalTo: parent.bottomAnchor, constant: -bottom).isActive = true
        }
    }
}
视图检查器的屏幕快照, 其中,子代->子代-&>子代是相机的视区

这篇关于将UIImagePickerController CameraOverlayView放置在相机预览的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)