Skip to content

Instantly share code, notes, and snippets.

@GoranLilja
Last active May 12, 2024 12:24
Show Gist options
  • Save GoranLilja/bbae67c641aef2cd38ef2379b1cc036b to your computer and use it in GitHub Desktop.
Save GoranLilja/bbae67c641aef2cd38ef2379b1cc036b to your computer and use it in GitHub Desktop.
Blur app when resigning active
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func applicationWillResignActive(_ application: UIApplication) {
addBlurViews()
}
func applicationDidBecomeActive(_ application: UIApplication) {
removeBlurViews()
}
}
private extension AppDelegate {
var blurViewTag: Int {
return 999999
}
func addBlurViews() {
for window in UIApplication.shared.windows {
let blurEffect: UIBlurEffect
if #available(iOS 13.0, *) {
blurEffect = UIBlurEffect(style: .systemUltraThinMaterial)
} else {
blurEffect = UIBlurEffect(style: .light)
}
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = window.frame
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
blurEffectView.tag = blurViewTag
window.addSubview(blurEffectView)
}
}
func removeBlurViews() {
for window in UIApplication.shared.windows {
if let blurView = window.viewWithTag(blurViewTag) {
blurView.removeFromSuperview()
}
}
}
}
@john-07
Copy link

john-07 commented May 12, 2024

thank

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