Last active
May 12, 2024 12:24
-
-
Save GoranLilja/bbae67c641aef2cd38ef2379b1cc036b to your computer and use it in GitHub Desktop.
Blur app when resigning active
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
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() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank