6

Is it any way to change blur effect style on-the-fly after creating view?

E.g. i've created in Storyboard blur view, but i need to change effect style from .Light to .Dark programmatically.

I can't just re-create view, because I have information in that view and dynamic constraints (constraints change with animations in some situations).

Vasily
  • 3,499
  • 3
  • 24
  • 56
  • i know how to add, i need to change effect without recreating view – Vasily Apr 23 '15 at 14:27
  • Hm... Looking at the docs, it seems like there's no way to change a blur effect on the fly. I'd recommend removing the entire UIVisualEffectView programmatically and initializing a new one with the other UIBlurEffect. – Lyndsey Scott Apr 23 '15 at 14:29
  • The `UIVisualEffectView` properties `effect` and `contentView` are read-only, but you might be able to re-use the `contentView`. – A-Live Apr 23 '15 at 14:31
  • Re: your edit. Maybe you shouldn't keep that info directly within the blur view then. Perhaps you could put it within a transparent view above the blur view. Or as A-Live suggested, perhaps there's a way to re-use the contentView and keep track of the dynamic constraints. – Lyndsey Scott Apr 23 '15 at 14:53

1 Answers1

12

yes, we can change the blur effect style on the fly... here is the code you are looking for....

MyView.effect = UIBlurEffect(style: .light) MyView.effect = UIBlurEffect(style: .dark) MyView.effect = UIBlurEffect(style: .extraLight)

Fansad PP
  • 429
  • 4
  • 11
  • Thank you Fansad for the proper solution.. guys please upvote for this solution. –  Nov 02 '18 at 09:50