0

I have a UITabBar, with the sections, "main" and "profile". In profile I use a background image that fills the entire screen, with a blur effect. It works pretty good. When I go back to main and go back again to profile, a new blur effect is added over the first blur effect.

If I place the code corresponding to set the blur effect in viewDidLoad() it works fine, the effect is added just one time. But there's a problem, the blur effect doesn't fill the entire screen. I suppose this is caused because in viewDidLoad nobody knows the frame of the imageView, so it fills a 3/4 parts of the image.

My question is: how can I fix this? supposing it is caused due to the image frame, that is unknown by now. How do I set the frame measures?

If you think it's caused by another thing tell me what can be.

this is the blur code I'm using?

self.picBlurView = UIVisualEffectView(effect: self.picBlur)
self.picBlurView.frame = self.profileSubView.bounds    
self.profileSubView.addSubview(self.picBlurView)

Many thanks.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Aitor Sola
  • 17
  • 5

2 Answers2

1

You can set a flag in your class like "_alreadyBlurred". You set it at NO in the viewDidLoad.

In viewWillAppear, you check if the flag == NO, if it does, you make the blur effect and set the flag = YES.

Then in the next call it wont add another blur effect, and in this method you know the frame .

Code :

in viewWillAppear :

if (_myBlurView == nil) {
    ....
    // do blur effect and add
    ....
}
David Ansermot
  • 6,052
  • 8
  • 47
  • 82
  • Many thanks. I think that's the solution! but I don't know how to set a flag and I don't know either how to check it out! What I've done to fix this is remove from superview the blur effect everytime the view disappear, it works, but I know this is not how it has to be done. – Aitor Sola Apr 17 '15 at 13:37
  • You do not need a new property. Just use self.picBlurView. If it has not been created, create and add. – Rory McKinnel Apr 17 '15 at 13:41
  • Yeah but you've suggered an edit on mine so I edited :P – David Ansermot Apr 17 '15 at 13:45
1

Since you already have picBlurView as a property, simply check if it exists. If does not exist, then create it and add it. Otherwise do nothing. Do this in viewWillAppear.

if (self.picBlurView == nil){
  self.picBlurView = UIVisualEffectView(effect: self.picBlur)
  self.picBlurView.frame = self.profileSubView.bounds
  self.profileSubView.addSubview(self.picBlurView)
}

You probably want to implement the method willAnimateRotationToInterfaceOrientation so that you can reapply the blur effect when rotation takes place if that matters to you. In here you just replace the one you added originally.

Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28