Search code examples
swiftimageviewuiblureffect

Blur Not matching the size of the image view Swift


enter image description here

I'm trying to create a blurry login screen and I looked up how to do it online. When I apply the constraints to the image view, (from what I understand/think) it doesnt apply to the blurred out part.

import UIKit

class ViewController: UIViewController {

    @IBOutlet var mainBackgroundImage: UIImageView!
    override func viewDidLoad() {
        self.view.layoutIfNeeded()
        self.blurMainImg()
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func blurMainImg(){
        let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.Dark)
        let blurView = UIVisualEffectView(effect: blurEffect)
        blurView.frame = mainBackgroundImage.bounds
        mainBackgroundImage.addSubview(blurView)
    }

}

enter image description here


Solution

  • As you have post in your picture seems it's a matter of constraints (leading and trailing) you must delete it and put trailing constraint to Superview = 0, the same for leading -> Superview = 0

    An example (look for trailing and leading):

    enter image description here

    P.S.: dont forgot to silent warning by update your constraints (tap the yellow triangle and flag for all)

    enter image description here