0

I've created a mask view for my camera screen that blurs everything except for a small rectangle in the middle. I'm having issues when testing on different screen sizes. I'm testing on an 11 Pro and an 8. If my storyboard view has the proper device selected, the constraints look fine on the device. But if I switch phones without changing in the storyboard, it is incorrect. I'm programmatically creating the mask view. Any help would be much appreciated!

func setupBlur() { if !UIAccessibility.isReduceTransparencyEnabled { blurView.backgroundColor = .clear let blurEffect = UIBlurEffect(style: .dark) let blurEffectView = UIVisualEffectView(effect: blurEffect) blurEffectView.frame = blurView.bounds blurEffectView.translatesAutoresizingMaskIntoConstraints = false blurEffectView.addConstraints(blurView.constraints) blurView.addSubview(blurEffectView) let maskView = UIView(frame: blurView.bounds) maskView.translatesAutoresizingMaskIntoConstraints = false maskView.addConstraints(blurView.constraints) maskView.backgroundColor = .clear let outerbezierPath = UIBezierPath.init(rect: blurView.bounds) let croppedOriginX = (blurView.bounds.width / 2) - (captureView.bounds.width / 2) let croppedOriginY = (blurView.bounds.height / 2) - (captureView.bounds.height / 2) let frame = CGRect(x: croppedOriginX, y: croppedOriginY, width: captureView.bounds.width, height: captureView.bounds.height) let innerRectPath = UIBezierPath.init(rect: frame) outerbezierPath.append(innerRectPath) outerbezierPath.usesEvenOddFillRule = true let fillLayer = CAShapeLayer() fillLayer.fillRule = kCAFillRuleEvenOdd fillLayer.fillColor = UIColor.green.cgColor // any opaque color would work fillLayer.path = outerbezierPath.cgPath maskView.layer.addSublayer(fillLayer) blurView.mask = maskView; } } 

enter image description here

enter image description here

3
  • 1
    once you have added the blurView, try calling view.layoutIfNeeded() . Commented Nov 22, 2019 at 17:44
  • 2
    I assume you;re trying to apply the same constraints to maskView as already exist on blurview by maskView.addConstraints(blurView.constraints) ? I don't think this will work as expected as the constraints are tied to the views/anchors they were created with, not generic anchors that will apply to any view. Try setting the constraints explicitly. Commented Nov 22, 2019 at 18:25
  • What are the constraints that are set on your blurView in your storyboard? Commented Nov 22, 2019 at 19:46

3 Answers 3

2

This happens because when you call setupBlur() from viewDidLoad, at the beginning blurView.bounds values are from stroyboard which you're working on it, and you are using these bounds to setup blurEffectView.frame before they are adjusted for your device's screen.

This is not the best solution, but I hope it will resolve your problem.

Try this:

override func viewDidLoad() { super.viewDidLoad() DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { self.setupBlur() } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!! You were correct. Based off your answer, I was able to find a cleaner solution. override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() self.setupBlur() }
0
override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() self.setupBlur() } 

Comments

0

In my case, I've tried viewDidLayoutSubviews() instead of DispatchQue but it didn't work. However, viewDidLoad() worked for me.

override func viewDidAppear() { self.setupBlur() } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.