Savitha Suresh
Savitha Suresh

Reputation: 341

blurred background in swift with clear subview

i want a blurred background. i have a subview for the main view . and i want the subview to not be affected by blur. What should i do?

Code:

let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.dark)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)

        blurEffectView.frame = view.bounds


 let vibrancyEffect = UIVibrancyEffect(blurEffect: blurEffect)
   let vibrancyEffectView = UIVisualEffectView(effect: vibrancyEffect)
        vibrancyEffectView.frame = view.bounds

       view.addSubview(blurEffectView)

        view.addSubview(vibrancyEffectView)


        FrameView = UIView()


        if let FrameView = FrameView {

            FrameView.layer.borderColor = UIColor.white.cgColor
            FrameView.layer.borderWidth = 2
 FrameView.frame=CGRect(x:30,y:(view.frame.height/2-50),width:view.frame.width-60,height:100)
            blurEffectView.contentView.addSubview(FrameView)


   }

So i want the view to be blurred and the FrameView to be clear.

This is what my app currently looks like.

Upvotes: 2

Views: 5976

Answers (2)

Kira
Kira

Reputation: 243

I had try a test project , and i add the blur to mainView first , then i add the frameView to mainView , i seems good and the frameView is not blur.

    let mainView = UIImageView.init(frame: self.view.bounds);
    mainView.image = UIImage.init(named: "a.png");
    let blurEffect = UIBlurEffect.init(style: .dark);
    let effectView = UIVisualEffectView.init(effect: blurEffect);
    effectView.frame = mainView.bounds;
    effectView.alpha = 1;

    let frameView = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: 200, height: 50));
    frameView.image = UIImage.init(named: "b.png");
    frameView.center = self.view.center;
    //add the blur view first
    mainView.addSubview(effectView);
    self.view.addSubview(mainView);
    self.view.addSubview(frameView);

enter image description here

Upvotes: 3

Dominik Bucher
Dominik Bucher

Reputation: 2167

You have to add the frame upper in you hiearchy, as you see, the BlurEffectView blurs every subview within it, so check that you are adding the subView to the main View of viewController and use func bringSubView to front... :)

Upvotes: 0

Related Questions