我正在我的应用中创建一个二维码扫描器。 它包括导航栏,QRcode摄像头和结果框。这就是我想要做的。 With navigation bar
但是当我在手机中运行时,导航栏位于相机框架后面。 Without navigation bar
我尝试将导航栏的插座插入到我的代码中并插入添加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
}
答案 0 :(得分:1)
尝试替换此:
view.layer.addSublayer(videoPreviewLayer!)
使用:
view.layer.insertSublayer(videoPreviewLayer!, below: qrCodeFrameView.layer) //Or below: messageLabel.layer
尽量不要将子图层添加到顶部,而只是将其插入已经存在的其他子视图下方,它将确保导航栏正确显示