Skip to content

Instantly share code, notes, and snippets.

@RxDx
Created December 19, 2017 10:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RxDx/1fbe8882d2dd3d6028e0db115526fb3d to your computer and use it in GitHub Desktop.
Save RxDx/1fbe8882d2dd3d6028e0db115526fb3d to your computer and use it in GitHub Desktop.
iOS Theme
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