Skip to content

Instantly share code, notes, and snippets.

@ricobeck
Created October 1, 2024 09:39
Show Gist options
  • Save ricobeck/c0f4e08421ecca13536ea61d2b941adb to your computer and use it in GitHub Desktop.
Save ricobeck/c0f4e08421ecca13536ea61d2b941adb to your computer and use it in GitHub Desktop.
Runtime change of app's tint color
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