如何将导航栏放在相机框架的顶部?

我在我的应用程序中创build一个QR码扫描仪。 它包括导航栏,QR码相机和结果框。 这是我想要做的。 随着导航栏

但是当我在手机中运行时,导航栏位于相机框架后面。 没有导航栏

我试图插入导航栏的sockets到我的代码,并插入添加view.bringSubview(toFront: navigationbar)但Xcode说,这不是一个UIview所以我得到了错误。 我是IOS开发新手,任何帮助将不胜感激。

这是我的代码的一部分:

  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 } 

尝试replace这个:

 view.layer.addSublayer(videoPreviewLayer!) 

附:

view.layer.insertSublayer(videoPreviewLayer!, below: qrCodeFrameView.layer) //Or below: messageLabel.layer

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