我应该如何正确地向AVCaptureVideoPreviewLayer添加约束?

时间:2014-06-28 05:57:37

标签: ios xcode swift avcapturesession

我的视图顶部有一个导航栏。我添加了一个AVCaptureVideoPreviewLayer但它的定位方式,导航栏底部和预览层顶部之间存在间隙。

    self.view.backgroundColor = UIColor.redColor()

    var navBarFrame = CGRectMake(0, 0, self.view.frame.width, 64.0)
    var navBar = UINavigationBar(frame: navBarFrame)
    var navItem = UINavigationItem()
    navItem.title = "zzzz"
    navBar.pushNavigationItem(navItem, animated: false)
    self.view.addSubview(navBar)


    // init device input
    var error: NSErrorPointer!
    var deviceInput: AVCaptureInput = AVCaptureDeviceInput.deviceInputWithDevice(captureDevice, error: error) as AVCaptureInput
    self.stillImageOutput = AVCaptureStillImageOutput()


    // init session
    self.session = AVCaptureSession()
    self.session.sessionPreset = AVCaptureSessionPresetPhoto
    self.session.addInput(deviceInput as AVCaptureInput)
    self.session.addOutput(self.stillImageOutput)


    // layer for preview
    var previewLayer: AVCaptureVideoPreviewLayer = AVCaptureVideoPreviewLayer.layerWithSession(self.session) as AVCaptureVideoPreviewLayer
    previewLayer.frame = self.view.bounds
    self.view.layer.addSublayer(previewLayer)

如何确保预览图层被限制在导航栏的底部?

1 个答案:

答案 0 :(得分:1)

使用您想要的约束在IB中设计视图。将预览图层添加到此视图中,您还需要在视图大小更改时更新图层大小,使用视图控制器中的layoutSubviews方法在大小更改时获取更新。