如何将导航栏置于相机框的顶部? [英] How to bring the navigation bar on top of the camera frame?

查看:68
本文介绍了如何将导航栏置于相机框的顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用程序中创建一个QR码扫描仪.它包括导航栏,QRcode摄像头和结果框.这是我想做的.使用导航栏

I am creating a QR code scanner in my app. It includes the navigation bar, QRcode camera and a result box. Here is what I want to do. With navigation bar

但是当我在手机中跑步时,导航栏位于相机框的后面.没有导航栏

But when I run in my phone, the navigation bar is behind the camera frame. Without navigation bar

我试图在我的代码中插入导航栏的出口并插入add view.bringSubview(toFront:navigationbar),但是Xcode表示它不是 UIview 所以我得到了错误.我是IOS开发的新手,我们将不胜感激.

I have tried to insert a outlet of the navigation bar into my code and insert add view.bringSubview(toFront: navigationbar) but Xcode said it is not a UIviewso I got the error. I am new to IOS development, any help will be appreciated.

这是我的代码的一部分:

Here is part of my code:

    var captureSession:AVCaptureSession?
    var videoPreviewLayer:AVCaptureVideoPreviewLayer?
    var qrCodeFrameView:UIView?
     @IBOutlet weak var messageLabel: UILabel!
    // Added to support different barcodes
    let supportedBarCodes = [AVMetadataObjectTypeQRCode, AVMetadataObjectTypeCode128Code, AVMetadataObjectTypeCode39Code, AVMetadataObjectTypeCode93Code, AVMetadataObjectTypeUPCECode, AVMetadataObjectTypePDF417Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypeAztecCode]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        let captureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)

        do {
            // Get an instance of the AVCaptureDeviceInput class using the previous device object.
            let input = try AVCaptureDeviceInput(device: captureDevice)

            // Initialize the captureSession object.
            captureSession = AVCaptureSession()
            // Set the input device on the capture session.
            captureSession?.addInput(input)

            // Initialize a AVCaptureMetadataOutput object and set it as the output device to the capture session.
            let captureMetadataOutput = AVCaptureMetadataOutput()
            captureSession?.addOutput(captureMetadataOutput)

            // Set delegate and use the default dispatch queue to execute the call back
            captureMetadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)

            // Detect all the supported bar code
            captureMetadataOutput.metadataObjectTypes = supportedBarCodes

            // Initialize the video preview layer and add it as a sublayer to the viewPreview view's layer.
            videoPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
            videoPreviewLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
            videoPreviewLayer?.frame = view.layer.bounds
            view.layer.addSublayer(videoPreviewLayer!)

            // Start video capture
            captureSession?.startRunning()

            // Move the message label to the top view
            view.bringSubview(toFront: messageLabel)

            // Initialize QR Code Frame to highlight the QR code
            qrCodeFrameView = UIView()

            if let qrCodeFrameView = qrCodeFrameView {
                qrCodeFrameView.layer.borderColor = UIColor.green.cgColor
                qrCodeFrameView.layer.borderWidth = 2
                view.addSubview(qrCodeFrameView)
                view.bringSubview(toFront: qrCodeFrameView)
            }

        } catch {
            // If any error occurs, simply print it out and don't continue any more.
            print(error)
            return
        }

推荐答案

尝试替换为:

view.layer.addSublayer(videoPreviewLayer!)

使用:

view.layer.insertSublayer(videoPreviewLayer !,以下:qrCodeFrameView.layer)//或以下:messageLabel.layer

尽量不要在子图层的顶部添加子图层,而只需将其插入已经存在的其他子视图下方,这样可以确保导航栏正确显示

Try not to add the sublayer to the top, instead, just insert it below some other subview that's already there, it will ensure the nav bar to show correctly

这篇关于如何将导航栏置于相机框的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆