Not able to get blur effect

136 views Asked by At

I'm using UIVisual Effect view in storyBorad. I just set four constraints(top, bottom, left, right) to that view. but it looks some black colour view behind.

source code:

import UIKit

class SignOutViewController: UIViewController {

    @IBOutlet weak var signouTitle: UILabel!
    @IBOutlet weak var desc1: UILabel!
    @IBOutlet weak var desc2: UILabel!
    @IBOutlet weak var cancelButton: UIButton!
    @IBOutlet weak var sureButton: UIButton!
    @IBOutlet weak var alertView: UIView!
    @IBOutlet weak var seperatorView: UIView!
    @IBOutlet weak var buttonsSuperView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareAlertView()
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        alertView.layer.cornerRadius = alertView.frame.height * 0.05

    }

    private func prepareAlertView() -> Void{
        signouTitle.text = "Sign Out"
        desc1.text = "Do you really want to sign out"
        desc2.text = "from this account"
        signouTitle.textColor = UIColor(red: 1 / 255, green: 170 / 255, blue: 112 / 255, alpha: 1)
        desc1.textColor = UIColor(red: 195 / 255, green: 195 / 255, blue: 195 / 255, alpha: 1)
        desc2.textColor = UIColor(red: 195 / 255, green: 195 / 255, blue: 195 / 255, alpha: 1)
        cancelButton.setAttributedTitle(NSAttributedString(string: "Cancel", attributes: [NSFontAttributeName: UIFont.getProximaNovaSemibold(size: 16), NSForegroundColorAttributeName: UIColor.white]), for: .normal)
        sureButton.setAttributedTitle(NSAttributedString(string: "Sure", attributes: [NSFontAttributeName: UIFont.getProximaNovaSemibold(size: 16), NSForegroundColorAttributeName: UIColor.white]), for: .normal)
        self.view.backgroundColor = UIColor.black.withAlphaComponent(0.65)
        buttonsSuperView.backgroundColor = UIColor(red: 1 / 255, green: 170 / 255, blue: 112 / 255, alpha: 1)
        alertView.backgroundColor = UIColor(red: 36 / 255, green: 36 / 255, blue: 40 / 255, alpha: 1)
    }

    @IBAction func cancelTapped(_ sender: Any) {
        self.dismiss(animated: true, completion: nil)
    }

    @IBAction func sureTapped(_ sender: Any) {
    }

}

I added UIVisual effectView Programatically. i need visualEffect as -1 th position in viewHierarchy. but it responds as same..

2

There are 2 answers

2
Vikas Rajput On

try this

let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(blurEffectView)
0
Moaz Khan On

I faced exactly the same issue. What I did was I just used a new storyboard and presented it modally using the settings in screenshot. The blur effect then works perfectly instead of showing the black screen. You can also translate that into code if you want to do it programatically.

Note: These settings are for the segue.

enter image description here