Search code examples
iosswiftuibuttonqr-code

iOS button is clickable but not visible


I have a view(let's call it scanView) that opens camera to scan for QR Codes. I have placed a button on it which opens or closes the flash. The problem i am facing is that the button is getting clicked but it is not visible, i,e, I can turn flash on or off by clicking on screen where i placed the button, but the button is not visible. I checked the Alpha of button, tried bringing subview to front also. Nothing works. Is it some issue related to QR reader of iOS? Here is the implementation of QR Reader:

        captureSession = AVCaptureSession()

        guard let videoCaptureDevice = AVCaptureDevice.default(for: .video) else { return }
        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 = [.qr]
        } else {
            failed()
            return
        }

        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
        previewLayer.frame = scanView.layer.bounds
        previewLayer.videoGravity = .resizeAspectFill
        
        scanView.layer.addSublayer(previewLayer)
        captureSession.startRunning()

view heirarchy debugger image

As you can see above I have added a background color and background image too for clarity. According to me the button looks fine in hierarchy. On this scanView, I am adding the button from my xib file


Solution

  • I managed to solve the issue by setting the zposition of the button. Thank you for your time everyone