ios - 强制图层不在选项卡栏中旋转

标签 ios swift swift2 avfoundation tabbarcontroller

我有一个 tabBar 应用程序。其中一个选项卡有一个 AVCaptureVideoPreviewLayer,我在其中显示了一个用于读取条形码的相机。当我旋转屏幕时,所有内容都会旋转(选项卡栏和相机图像)。我想要得到的是相机的静态图像(禁用其旋转)和旋转屏幕时旋转的选项卡栏。

这就是我将相机图层添加到 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 = AVCaptureVideoPreviewLayer(session: self.captureSession);
    self.previewLayer.frame = self.view.layer.bounds;
    self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    self.view.layer.addSublayer(self.previewLayer)
    self.captureSession.startRunning();

}

最佳答案

停止 View 而不是图层的旋转。

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

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

这应该有效。

关于ios - 强制图层不在选项卡栏中旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35269701/

相关文章:

ios - WKWebView Inside View 和 Inside ScrollView 失去联系 iOS 9

ios - 使用 faSTLane/gym 导出未签名的 ipa

ios - 如何在 UICollectionview 中显示前 9 个图像?

ios - 如何从 Cocos2d CCLayer 中删除这些信息?

ios - 通知中心 : BAD_ACCESS when Notification postet

ios - 将异步 Gif 加载到滚动的 UICollectionView

ios - 如何在swift中自动重复功能

ios - 如何清除json中的双引号?

uiviewcontroller - 将 "quick actions"添加到我的 iOS 9 应用程序

ios - 如何动态填充标签栏 Controller 的标签栏。? ( swift ——iOS 9)