Easy way to switch between themes using swizzle to catch app colors and return the selected theme color.
-
copy UIColorThemeExtension.swift, asset-to-color-extension.stencil and swiftgen.yml to your project
-
make sure that asset-to-color-extension.stencil and swiftgen.yml in the same folder
-
change folder directory for output and inputs in swiftgen.yml
-
install SwiftGen
-
run SwiftGen in your project
-
SwiftGen will generat UIColorExtension.swift in output directory for you. this file must have
colorsNames
to get all colors in your Colors.xcassets assets file.
-
call
UIColor.swizzleInitImplementation
with themes of your app in the begining. -
to add colors for your theme, you have to use this formate
colorName_themeName
colorName is the name of color themeName is theme name -
call
UIColor.switchToTheme
with theme name when you want to switch theme. if you want to return to default theme set theme name as emptyUIColor.switchToTheme("")
-
to see the effect of theme switch you have to reset your window