嗨,我正在尝试编写一些带有主题切换器的 iPhone 应用程序,用户可以在其中选择一个主题来更改背景颜色、Alpha、图像以及一些按钮的外观和感觉(大小、图像,甚至地点)。
应用主题的最佳方式是什么?
谢谢, 蒂姆
最佳答案
以下是我如何在 FemCal 中实现更改主题的功能。我以代码片段的形式包含了一些详细信息。
创建一个单例 ThemeMgr 类来存储颜色、图像等。需要时获取单例。
@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; }
使用通知来广播主题更改。我使用@“ThemeChange”作为通知。
显然,您将有一些 UI 来选择所需的主题。 在本例中,用户选择一个主题并- (void)fireTimer:(NSTimer *)timer { NSNotification * themeChange = [NSNotification notificationWithName:@"ThemeChange" object:nil]; [[NSNotificationQueue defaultQueue] enqueueNotification:themeChange postingStyle:NSPostWhenIdle]; }
fireTimer
0.5 秒后触发。在我强制 UI 自行重绘之前,这为其他 UI 的更新提供了很好的延迟。在您需要对主题更改采取行动的任何地方监听通知。
我只有几个 View ,因此我在我使用的每个 Controller 中编写了代码,但您可以使用 Objective-C 的强大功能来混合代码,以更好的方式处理这个问题。里>// listen for change notification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateAppearance:) name:@"ThemeChange" object:nil];
实现代码以根据主题实际重绘 View 。
- (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]; }
不要忘记在必要时放弃任何通知,并且您必须在 viewDidLoad 或类似的代码中编写代码才能在显示 View 之前应用任何主题。
关于iphone - 将主题应用到 iPhone 应用程序的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2730926/