Skip to content

Instantly share code, notes, and snippets.

@KyleGoslan
Last active August 9, 2023 08:47
Show Gist options
  • Save KyleGoslan/4f0dd3a387c2387c31ccf823f2bb94d9 to your computer and use it in GitHub Desktop.
Save KyleGoslan/4f0dd3a387c2387c31ccf823f2bb94d9 to your computer and use it in GitHub Desktop.
UIView extension to easily add and remove a blur (UIVisualEffectView)
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()
}
}
}
}
@hpp-hash
Copy link

I get an error "Type 'UIVisualEffectView?' does not conform to protocol 'Sequence'" How can I fix this? Thanks

@KyleGoslan
Copy link
Author

@hp0101 I've updated for Swift 4.2.

@OksanaFedorchuk
Copy link

Thanks!

@mutsm
Copy link

mutsm commented Aug 9, 2023

I replaced "self.subviews" with "view.subviews"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment