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) -
[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).