2

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.

Nishant Bhindi
  • 2,242
  • 8
  • 21
Savitha Suresh
  • 321
  • 4
  • 12
  • do like create one UIView -- add your blurEffectView to that custom UIVIew add your FrameView to mainUIVIewcontroller.view, change the View hirarchy surely works – Anbu.Karthik May 31 '17 at 06:31
  • 1
    Possible duplicate of [Adding blur effect to background in swift](https://stackoverflow.com/questions/30953201/adding-blur-effect-to-background-in-swift) – MGY May 31 '17 at 06:35

2 Answers2

3

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

Kira
  • 243
  • 2
  • 9
0

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

Dominik Bucher
  • 2,120
  • 2
  • 16
  • 25