Created
October 1, 2024 09:39
-
-
Save ricobeck/c0f4e08421ecca13536ea61d2b941adb to your computer and use it in GitHub Desktop.
Runtime change of app's tint color
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
struct HammerHeart: SwiftUI.App { | |
@UIApplicationDelegateAdaptor private var delegate: AppDelegate | |
@Shared(.appTintColor) var appTintColor: Color | |
var body: some Scene { | |
WindowGroup { | |
AppView(store: AppDelegate.store) | |
.accentColor(appTintColor) | |
.tint(appTintColor) | |
.onChange(of: appTintColor) { | |
UIView.appearance().tintColor = UIColor(appTintColor) | |
UINavigationBar.appearance().tintColor = UIColor(appTintColor) | |
UITabBar.appearance().tintColor = UIColor(appTintColor) | |
UIApplication.shared.connectedScenes.forEach { scene in | |
if let windowScene = scene as? UIWindowScene { | |
windowScene.windows.forEach { window in | |
window.tintColor = UIColor(appTintColor) | |
window.reload() | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
private extension UIWindow { | |
func reload() { | |
subviews.forEach { view in | |
view.removeFromSuperview() | |
addSubview(view) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment