我想在条形码扫描仪中添加导航栏。通过这种方式,我可以关闭条形码扫描仪(模态)视图,但由于导航栏不在此处,因此无法正常工作。
这些是我的setupNavBar方法和我的navBar来设置我的navigationBar:
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
}
提前谢谢你。干得好
答案 0 :(得分:1)
前几天我遇到了同样的问题。我创建了一个scannerviewcontroller
。
检查一下:https://gist.github.com/Raghvendra7/ff6335c47bbca04fb88d1cb76917d2e5,
它解决了取消问题。
希望它会有所帮助。
在此控制器中处理扫描仪效果,并在完成时更新。
答案 1 :(得分:0)
我发现了我的错误。在我的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();
}