The best way to apply a theme to an iPhone application

Hi, I am trying to write an application for the iPhone using a theme switcher where users can select a theme to change the background color, alpha, image and some buttons and appearance (size, image or even location).

What would be the best way to apply the theme?

Thanks Tim

+4
source share
2 answers

This is how I implemented the ability to change themes in FemCal. I have included some details in the form of code snippets.

  • Create a singleton ThemeMgr class that stores colors, images, etc. Choose a singleton if necessary.

     @interface ThemeMgr : NSObject { // selected color and image NSString * selectedColor; NSString * selectedPicture; // dictionaries for color and image NSDictionary * colorData; NSDictionary * imageData; NSDictionary * backgroundData; // names NSArray * colors; NSArray * images; // themes UIColor * tintColor; UIImageView * panelTheme; UIColor * tableBackground; } 
  • Use notifications to broadcast theme changes. As a notification, I used @ThemeChange.

     - (void)fireTimer:(NSTimer *)timer { NSNotification * themeChange = [NSNotification notificationWithName:@"ThemeChange" object:nil]; [[NSNotificationQueue defaultQueue] enqueueNotification:themeChange postingStyle:NSPostWhenIdle]; } 
    Obviously, you will have some user interface to select the theme you want. In this case, the user selects a theme, and fireTimer starts after 0.5 seconds. This gives a good delay for updating another user interface before I force the user interface to redraw itself.
  • Listen to notifications wherever you need to act when a topic changes.

     // listen for change notification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateAppearance:) name:@"ThemeChange" object:nil]; 
    I only have a few views, so I wrote the code in every controller that I use, but you can use the objective-C power to mix the code to better control this way.
  • Embed the code to actually redraw your presentation based on the theme.

     - (void)updateAppearance:(NSNotification *)notification { // background for grouped table view ThemeMgr * themeMgr = [ThemeMgr defaultMgr]; // change color and reload [self.tableView setBackgroundColor:[themeMgr tableBackground]]; [self.tableView reloadData]; self.navigationController.navigationBar.tintColor = [themeMgr tintColor]; } 

Do not forget to cancel any notifications when necessary, and you will have to write code in viewDidLoad or similarly apply any themes before the view is displayed.

+6
source

I have no answers. I implemented it using events and singleton. Basically, the singleton installation object sends changes to observers who update the GUI on events. I remember that there is a way in which you can listen to the change of the instance variable, but forgot how to do it. My current way works for me very well.

0
source

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


All Articles