How to create a theme-based application?

I know that a similar question has already been asked, but I have not received a satisfactory answer. So I am adding this question again.

Based on the user's choice in the iphone application, I need to change the appearance of the application (color background images of fonts, etc.). Is there a standard way to achieve this?

One possible solution would be to duplicate the xib files for each theme and download them based on the selection. Is this a good approach? mainly because posting outputs and actions for xib copies is an unnecessary task.

I would like to receive an expert suggestion on this doubt. Thanks for any help in advance.

-mia

+6
source share
3 answers

For a route without XIB

Color: iOS 5 provides new appearance APIs. Most user interface elements have the ability to set the colors of tintColor or backgroundColor. Rarely, even a background image.

Font: It could always change, but you could not animate it.

Background image: your main UIView should have an option for the background image, or if that fails, the background color of the template image.

0
source

The best way to implement a theme-based iOS application (without using the ~ xib route). you must create a build.xml file that will contain all the themes. You must keep your layout the same, and you can change the images, style, fonts, lang, views, in this layout according to pref users preferences. By doing this, you are not duplicating any code, and you will achieve better results in your application.

0
source

As CodaFI said, if you use iOS 5 or higher, you already have a theme feature. It works as follows:

  • Add the <IAppearanceContainer> protocol 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 a UINavigationBar , but it will work with any custom class. To view 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;//' 

Suppose you want modular themes:

  • Write a large plist file with all fonts, colors, music, and what not. Each of these files will be a theme.
  • Read the file through singleton and set values ​​with lines such as [x appearance] setWhatever:<plist value>] for each element capable of theme.
  • If you have instances of the same element that should display different elements, get these images through singleton.

This is more or less a tip from Amita Vyavahara. I said plist instead of xml because they are easier to read. Do not duplicate NIB unless you really need to.

0
source

Source: https://habr.com/ru/post/905712/


All Articles