Last active
August 9, 2023 08:47
-
-
Save KyleGoslan/4f0dd3a387c2387c31ccf823f2bb94d9 to your computer and use it in GitHub Desktop.
UIView extension to easily add and remove a blur (UIVisualEffectView)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
extension UIView { | |
func blurView(style: UIBlurEffect.Style) { | |
var blurEffectView = UIVisualEffectView() | |
let blurEffect = UIBlurEffect(style: style) | |
blurEffectView = UIVisualEffectView(effect: blurEffect) | |
blurEffectView.frame = bounds | |
addSubview(blurEffectView) | |
} | |
func removeBlur() { | |
for view in self.subviews { | |
if let view = view as? UIVisualEffectView { | |
view.removeFromSuperview() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I get an error "Type 'UIVisualEffectView?' does not conform to protocol 'Sequence'" How can I fix this? Thanks