Skip to content

Instantly share code, notes, and snippets.

@iAmrSalman
Created September 2, 2017 10:50
Show Gist options
  • Save iAmrSalman/3cf5bafd95d5d772bb12434542fab17d to your computer and use it in GitHub Desktop.
Save iAmrSalman/3cf5bafd95d5d772bb12434542fab17d to your computer and use it in GitHub Desktop.
[Theme Manager] #theme #swift3 #apperance
import UIKit
enum Theme: Int {
case dark, light
var dark: UIColor {
switch self {
case .dark:
return UIColor().colorFromHexString("212121")
case .light:
return UIColor().colorFromHexString("E0E0E0")
}
}
var background: UIColor {
switch self {
case .dark:
return UIColor().colorFromHexString("303030")
case .light:
return UIColor().colorFromHexString("EEEEEE")
}
}
var light: UIColor {
switch self {
case .dark:
return UIColor().colorFromHexString("424242")
case .light:
return UIColor().colorFromHexString("FFFFFF")
}
}
//Customizing the Navigation Bar
var barStyle: UIBarStyle {
switch self {
case .dark:
return .default
case .light:
return .black
}
}
var navigationBackgroundImage: UIImage? {
return self == .dark ? UIImage(named: "navBackground") : nil
}
var tabBarBackgroundImage: UIImage? {
return self == .dark ? UIImage(named: "tabBarBackground") : nil
}
var mainColor: UIColor {
switch self {
case .dark:
return UIColor().colorFromHexString("ffffff")
case .light:
return UIColor().colorFromHexString("000000")
}
}
var backgroundColor: UIColor {
switch self {
case .dark:
return UIColor().colorFromHexString("ffffff")
case .light:
return UIColor().colorFromHexString("000000")
}
}
var secondaryColor: UIColor {
switch self {
case .dark:
return UIColor().colorFromHexString("ffffff")
case .light:
return UIColor().colorFromHexString("000000")
}
}
var titleTextColor: UIColor {
switch self {
case .dark:
return UIColor().colorFromHexString("ffffff")
case .light:
return UIColor().colorFromHexString("000000")
}
}
var subtitleTextColor: UIColor {
switch self {
case .dark:
return UIColor().colorFromHexString("ffffff")
case .light:
return UIColor().colorFromHexString("000000")
}
}
}
class ThemeManager {
// ThemeManager
static func currentTheme() -> Theme {
if let storedTheme = (UserDefaults.standard.value(forKey: Keys.selectedTheme) as AnyObject).integerValue {
return Theme(rawValue: storedTheme)!
} else {
return .light
}
}
static func applyTheme(theme: Theme) {
UserDefaults.standard.setValue(theme.rawValue, forKey: Keys.selectedTheme)
UserDefaults.standard.synchronize()
let sharedApplication = UIApplication.shared
sharedApplication.delegate?.window??.tintColor = theme.mainColor
UINavigationBar.appearance().barStyle = theme.barStyle
UINavigationBar.appearance().setBackgroundImage(theme.navigationBackgroundImage, for: .default)
UITabBar.appearance().barStyle = theme.barStyle
UITabBar.appearance().backgroundImage = theme.tabBarBackgroundImage
let tabIndicator = UIImage(named: "tabBarSelectionIndicator")?.withRenderingMode(.alwaysTemplate)
let tabResizableIndicator = tabIndicator?.resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: 2.0, bottom: 0, right: 2.0))
UITabBar.appearance().selectionIndicatorImage = tabResizableIndicator
let controlBackground = UIImage(named: "controlBackground")?.withRenderingMode(.alwaysTemplate)
.resizableImage(withCapInsets: UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3))
let controlSelectedBackground = UIImage(named: "controlSelectedBackground")?
.withRenderingMode(.alwaysTemplate)
.resizableImage(withCapInsets: UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3))
UISegmentedControl.appearance().setBackgroundImage(controlBackground, for: .normal, barMetrics: .default)
UISegmentedControl.appearance().setBackgroundImage(controlSelectedBackground, for: .selected, barMetrics: .default)
UIStepper.appearance().setBackgroundImage(controlBackground, for: .normal)
UIStepper.appearance().setBackgroundImage(controlBackground, for: .disabled)
UIStepper.appearance().setBackgroundImage(controlBackground, for: .highlighted)
UIStepper.appearance().setDecrementImage(UIImage(named: "fewerPaws"), for: .normal)
UIStepper.appearance().setIncrementImage(UIImage(named: "morePaws"), for: .normal)
UISlider.appearance().setThumbImage(UIImage(named: "sliderThumb"), for: .normal)
UISlider.appearance().setMaximumTrackImage(UIImage(named: "maximumTrack")?
.resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: 0.0, bottom: 0, right: 6.0)), for: .normal)
UISlider.appearance().setMinimumTrackImage(UIImage(named: "minimumTrack")?
.withRenderingMode(.alwaysTemplate)
.resizableImage(withCapInsets: UIEdgeInsets(top: 0, left: 6.0, bottom: 0, right: 0)), for: .normal)
UISwitch.appearance().onTintColor = theme.mainColor.withAlphaComponent(0.3)
UISwitch.appearance().thumbTintColor = theme.mainColor
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment