How to create a theme based application?
Asked Answered
H

3

7

I know similar question is already asked, but didn't get a satisfactory answer. So i am adding this question again .

Based on user selection in iphone application , i need to change the look and feel of the application (color font background images etc ). Is there any standard way to achieve this ?

one possible solution can be duplicating xib files for each theme and loading it based on selection. Is this a good approach? mainly because wiring the outlets and actions for xib copies sounds to be a redundant task.

I would like to see expert suggestion for this doubt. Thanks for any help in advance.

-mia

Harrie answered 11/1, 2012 at 13:42 Comment(0)
F
0

For the non-XIB route

Color: iOS 5 exposes the new appearance APIs. Most UI elements have the option if setting a tintColor or a backgroundColor. Rarely, even a background image.

Font: Was always able to change, but you couldn't animate it.

Background image: Your main UIView should have the option for a background image, or if that fails, a background color with a pattern image.

Fellow answered 11/1, 2012 at 13:48 Comment(2)
This is just dynamically setting color based on selection. Thats fine, but i am looking for an efficient option like themes or styles where i can simply switch the themes dynamically at the same time i do not have to explicitly handle in code or duplicate nibs.Harrie
[UIButton setTintColor:[UIColor someColor]]; you have to handle everything yourself. Make a couple of methods with some preset color values for each 'new style' as you call itFellow
H
0

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.

Hazen answered 24/2, 2013 at 11:49 Comment(0)
M
-1

Best way to implement theme based application IOS application (not using ~xib route). you should create a build.xml file which will content all the pieces of themes. You should keep your layout same and you can change images, style, fonts, lang, views, in that layout according to users pref. By doing this you are not duplicating any code and you will achieve best performances from your application.

Marcosmarcotte answered 11/1, 2012 at 13:58 Comment(1)
Sorry i do not understand your explanation. Could you please explain a little more . Also this is not dynamic rt? means when the program is up and running user cannot switch themes.Harrie

© 2022 - 2024 — McMap. All rights reserved.