【问题标题】:Navigation Bar in Scanner View - IOS - Swift扫描仪视图中的导航栏 - IOS - Swift
【发布时间】:2016-09-19 11:49:26
【问题描述】:

我想在我的条码扫描仪视图中添加导航栏。通过这种方式,我可以关闭条码扫描器(模态)视图,但由于导航栏不在这里,某些东西不起作用。

这些是我的 setupNavBar 方法和用于设置我的导航栏的导航栏:

let navBar: UINavigationBar = {
        let bar = UINavigationBar()
        bar.translatesAutoresizingMaskIntoConstraints = false
        return bar
    }()

    func setupNavBar(){

        view.addSubview(navBar)
        view.bringSubview(toFront: navBar)

        navBar.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.09).isActive = true
        navBar.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
        navBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        navBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
        navBar.topAnchor.constraint(equalTo: view.topAnchor).isActive = true

        let doneItem = UIBarButtonItem(title:"Cancel",style: .done, target: nil, action: #selector(doneButton));
        let navItem = UINavigationItem(title: "Scanner");
        navItem.rightBarButtonItem = doneItem;
        navBar.setItems([navItem], animated: false);

    }


func doneButton(){

    let presentingViewController: UIViewController! = self.presentingViewController
    presentingViewController.presentingViewController!.dismiss(animated: true, completion: nil)

}

下面是我的扫描仪代码:

    var captureSession: AVCaptureSession!
    var previewLayer: AVCaptureVideoPreviewLayer!


    override func viewDidLoad() {
        super.viewDidLoad()

        setupNavBar()
        view.backgroundColor = UIColor.black
        captureSession = AVCaptureSession()

        let videoCaptureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
        let videoInput: AVCaptureDeviceInput

        do {
            videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
        } catch {
            return
        }

        if (captureSession.canAddInput(videoInput)) {
            captureSession.addInput(videoInput)
        } else {
            failed();
            return;
        }

        let metadataOutput = AVCaptureMetadataOutput()

        if (captureSession.canAddOutput(metadataOutput)) {
            captureSession.addOutput(metadataOutput)

            metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
            metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypePDF417Code]
        } else {
            failed()
            return
        }

        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession);
        previewLayer.frame = view.layer.bounds;
        previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
        view.layer.addSublayer(previewLayer);



        captureSession.startRunning();
    }

    func failed() {
        let ac = UIAlertController(title: "Scanning not supported", message: "Your device does not support scanning a code from an item. Please use a device with a camera.", preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "OK", style: .default))
        present(ac, animated: true)
        captureSession = nil
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if (captureSession?.isRunning == false) {
            captureSession.startRunning();
        }
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)

        if (captureSession?.isRunning == true) {
            captureSession.stopRunning();
        }
    }

    func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
        captureSession.stopRunning()

        if let metadataObject = metadataObjects.first {
            let readableObject = metadataObject as! AVMetadataMachineReadableCodeObject;

            AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
            found(code: readableObject.stringValue);

            self.isbn = readableObject.stringValue
            self.performSegue(withIdentifier: "scanToSell", sender: nil)
        }

    }

    func found(code: String) {
        print(code)
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        return .portrait
    }

提前谢谢你。干得好

【问题讨论】:

    标签: ios swift uinavigationcontroller uinavigationbar barcode-scanner


    【解决方案1】:

    几天前我遇到了同样的问题。我创建了一个scannerviewcontroller。 检查这个:https://gist.github.com/Raghvendra7/ff6335c47bbca04fb88d1cb76917d2e5, 它解决了取消问题。 希望它会有所帮助。 在此控制器中处理扫描仪效果,并将在完成后更新。

    【讨论】:

      【解决方案2】:

      我发现了我的错误。在我的viewDidLoad 方法中,setupNavBar 必须以这种方式改变位置。

       override func viewDidLoad() {
          super.viewDidLoad()
      
        //  setupNavBar() No
          view.backgroundColor = UIColor.black
          captureSession = AVCaptureSession()
      
          let videoCaptureDevice = AVCaptureDevice.defaultDevice(withMediaType: AVMediaTypeVideo)
          let videoInput: AVCaptureDeviceInput
      
              do {
                  videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
              } catch {
                  return
              }
      
              if (captureSession.canAddInput(videoInput)) {
                  captureSession.addInput(videoInput)
              } else {
                  failed();
                  return;
              }
      
              let metadataOutput = AVCaptureMetadataOutput()
      
              if (captureSession.canAddOutput(metadataOutput)) {
                  captureSession.addOutput(metadataOutput)
      
                  metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
                  metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypePDF417Code]
              } else {
                  failed()
                  return
              }
      
              previewLayer = AVCaptureVideoPreviewLayer(session: captureSession);
              previewLayer.frame = view.layer.bounds;
              previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
              view.layer.addSublayer(previewLayer);
      
        setupNavBar() // Yes
      
              captureSession.startRunning();
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-02
        • 2020-05-18
        相关资源
        最近更新 更多