-5

If I create a UIView with a frame like - UIView(frame:..) AND also set constraints on this view will the frame dimensions take precedence over the constraints? Will iOS runtime ignore the constraints?

I created a UIView in viewDidLoad() with a frame and attached constraints to this view. However the constraints are not being enforced at all and the view is rendered in the frame passed to the initializer.

[Edit]

My code is below. I am trying to add an overlay to AVCaptureVideoPreviewLayer (viewPreviewLayer) programatically on top of a UIView (previewContainer, configured with constraints on storyboard) that covers the entire screen.

....

var videoPreviewLayer:AVCaptureVideoPreviewLayer!
   @IBOutlet weak var previewContainer: UIView!
   ....
    override func viewDidLoad() {
      .....
      self.configurePreviewLayer()
    }

 override func viewDidLayoutSubviews() {
        print("viewDidLayoutSubviews")
        super.viewDidLayoutSubviews()
        self.videoPreviewLayer.frame = self.previewContainer.bounds

    }

    func configurePreviewLayer() {
            print("configurePreviewLayer()")
            self.videoPreviewLayer = AVCaptureVideoPreviewLayer(session: self.captureSession)
            self.videoPreviewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
            self.previewContainer.layer.addSublayer(self.videoPreviewLayer)

            configureBlurView()
        }

func configureBlurView() {
        print("configureBlurView()")

        // Blur View

        let viewFinderRadius:CGFloat = self.view.bounds.width / 2.0 - 16.0

        print("configureBlurView:\(self.previewContainer.bounds.size.height)")

        let blurView = createOverlay(frame:self.previewContainer.bounds,xOffset: self.view.bounds.size.width / 2.0, yOffset: self.view.bounds.size.height / 2.0, radius: viewFinderRadius)

        blurView.translatesAutoresizingMaskIntoConstraints = false

        self.previewContainer.addSubview(blurView)

        self.blurView = blurView

        blurView.leftAnchor.constraint(equalTo: self.previewContainer.leftAnchor).isActive = true
        blurView.topAnchor.constraint(equalTo:self.previewContainer.topAnchor).isActive = true
        blurView.rightAnchor.constraint(equalTo:self.previewContainer.rightAnchor).isActive = true
        blurView.bottomAnchor.constraint(equalTo:self.previewContainer.bottomAnchor).isActive = true

}

func createOverlay(frame : CGRect, xOffset: CGFloat, yOffset: CGFloat, radius: CGFloat) -> UIView
    {
        let overlayView = UIView(frame: frame)

        overlayView.backgroundColor = UIColor.black.withAlphaComponent(0.7)

        let path = CGMutablePath()
        path.addArc(center: CGPoint(x: xOffset, y: yOffset), radius: radius, startAngle: 0.0, endAngle: 2.0 * CGFloat.pi, clockwise: false)
        path.addRect(CGRect(origin: .zero, size: overlayView.frame.size))

        let maskLayer = CAShapeLayer()
        maskLayer.backgroundColor = UIColor.black.cgColor
        maskLayer.path = path
        maskLayer.fillRule = kCAFillRuleEvenOdd

        overlayView.layer.mask = maskLayer
        overlayView.clipsToBounds = true

        return overlayView
    }

Here is how the rendered screen looks (As you can see the overlay doesn't cover the entire screen or in other words the entire bounds of previewContainer) -

enter image description here

[Update]

I may have found the culprit. It is this piece of code that masks the overlay -

let path = CGMutablePath()
        path.addArc(center: CGPoint(x: xOffset, y: yOffset), radius: radius, startAngle: 0.0, endAngle: 2.0 * CGFloat.pi, clockwise: false)
        path.addRect(CGRect(origin: .zero, size: overlayView.frame.size))

        let maskLayer = CAShapeLayer()
        maskLayer.backgroundColor = UIColor.black.cgColor
        maskLayer.path = path
        maskLayer.fillRule = kCAFillRuleEvenOdd
        overlayView.layer.mask = maskLayer

If I remove the above mask everything is rendered properly. Now I need to figure out how to draw that mask (view finder for the camera).

Cœur
  • 37,241
  • 25
  • 195
  • 267
cubsnlinux
  • 365
  • 3
  • 15

1 Answers1

0

Will iOS runtime ignore the constraints?

If the constraints are set properly, no: it will ignore the frame. However, you have not shown what you did, so it is impossible to say what you are seeing and why.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • please see above. I updated my post with code and a screenshot. – cubsnlinux Dec 21 '17 at 20:40
  • well, your issue is far more involved than the question would seem to indicate; basically you've asked an x-y question (asking about an _assumption_ as to what the problem is, rather than asking about the actual problem) – matt Dec 21 '17 at 20:48
  • Seems like it. Can you let me know what am I doing wrong to get to what I intend to create? As you can see I want to create an overlay over the camera view. I would also like to attach labels etc on top of the overlay eventually. – cubsnlinux Dec 21 '17 at 20:54
  • The reason I made that assumption is because the overlay rendered properly when I removed the frame from the initializer. It may be correlation by causation but not a wild assumption. Any how any help is greatly appreciated. – cubsnlinux Dec 21 '17 at 22:07
  • I still don't understand. If you know how to make it work, then make it work. – matt Dec 21 '17 at 22:08