angeldev February 2016

Force a layer not to rotate in a tabbar

I have a tabBar app. One of the tabs has a AVCaptureVideoPreviewLayer, I display in it a camera for reading barcodes. When I rotate the screen, everything rotate (tabbar and image of the camera). What I would like to get is a static image of the camera (disable its rotation) and a tabbar rotating when I rotate the screen.

This is how I add the layer of the camera to the tabBar:

override func viewDidLoad() {

    super.viewDidLoad()

    view.backgroundColor = UIColor.blackColor()
    self.captureSession = AVCaptureSession()

    // This object represents a physical capture device and the properties associated with that device
    let videoCaptureDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeVideo)
    // Is useful for capturing the data from the input device
    let videoInput: AVCaptureDeviceInput

    do {
        videoInput = try AVCaptureDeviceInput(device: videoCaptureDevice)
    } catch {
        self.failed();
        return
    }

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

    let metadataOutput = AVCaptureMetadataOutput()

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

        metadataOutput.setMetadataObjectsDelegate(self, queue: dispatch_get_main_queue())
        metadataOutput.metadataObjectTypes = [AVMetadataObjectTypeEAN8Code, AVMetadataObjectTypeEAN13Code, AVMetadataObjectTypePDF417Code,
            AVMetadataObjectTypeUPCECode, AVMetadataObjectTypeCode39Code,
            AVMetadataObjectTypeCode39Mod43Code, AVMetadataObjectTypeCode93Code,
            AVMetadataObjectTypeCode128Code]
    } else {
        self.failed()
        return
    }

    // Adds the preview layer to display the captured data. Sets the videoGravity to AspectFill so that it covers the full screen
    self.previewLayer = AVCaptureVideoPrevie        

Answers


TechBee February 2016

Stop rotation of the view not layer.

override func shouldAutorotate() -> Bool {
   return false
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
   return UIInterfaceOrientation.Portrait.rawValue
}

This should work.

Post Status

Asked in February 2016
Viewed 3,399 times
Voted 7
Answered 1 times

Search




Leave an answer