Created
December 19, 2017 10:04
-
-
Save RxDx/1fbe8882d2dd3d6028e0db115526fb3d to your computer and use it in GitHub Desktop.
iOS Theme
This file contains hidden or 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
import UIKit | |
enum Theme { | |
case light | |
case dark | |
} | |
extension Theme { | |
var defaultTextColor: UIColor { | |
switch self { | |
case .light: | |
return .black | |
case .dark: | |
return .white | |
} | |
} | |
} | |
let ThemeDidChangeNotificationThemeUserInfoKey = "theme" | |
extension Notification.Name { | |
static let ThemeDidChange = Notification.Name("ThemeDidChange") | |
} | |
NotificationCenter.default.post(name: .ThemeDidChange, object: nil, userInfo: [ | |
ThemeDidChangeNotificationThemeUserInfoKey : Theme.dark, | |
]) | |
class View: UIView { | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange(_:)), name: .ThemeDidChange, object: nil) | |
} | |
@objc private func themeDidChange(_ notification: Notification) { | |
self.theme = notification.userInfo![ThemeDidChangeNotificationThemeUserInfoKey] as! Theme | |
} | |
private var theme: Theme = .light { | |
didSet { | |
self.textLabel.textColor = self.theme.defaultTextColor | |
// ... | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment