As CodaFI said, if you are using iOS 5 or greater, you already have a theming feature. It works like this:
Add the protocol <IAppearanceContainer>
to the class.
Decorate the property you intend to change with UI_APPEARANCE_SELECTOR
. Example:
@interface UINavigationBar : ... <IAppearanceContainer>
@property(nonatomic,retain) UIColor *tintColor UI_APPEARANCE_SELECTOR;
@end
- Change the color for all instances of the class:
[[UINavigationBar appearance] setTintColor:[UIColor redColor]];
The example above is the UINavigationBar
, but it would work with any custom class. To see the objects already supported in iOS 6.1, check the documentation or run the following commands:
cd /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS6.1.sdk/System/Library/Frameworks/UIKit.framework/Headers
grep -H UI_APPEARANCE_SELECTOR ./* | sed 's/ __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR;//'
Let's say you want modular themes:
- Write a big plist file with all the fonts, colors, music, and what not. Each of these files will be a theme.
- Read the file through a singleton and set the values with lines like
[x appearance] setWhatever:<plist value>]
for each theme-able element.
- If you have instances of the same element that need to display different elements, get those images through the singleton.
That is more or less the tip from Amit Vyawahare. I said plist instead xml because they are easier to read. Don't duplicate the NIBs unless you really have to.