您好,我正在尝试编写一些带有主题切换器的 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”作为通知。
- (void)fireTimer:(NSTimer *)timer
{
NSNotification * themeChange = [NSNotification notificationWithName:@"ThemeChange" object:nil];
[[NSNotificationQueue defaultQueue] enqueueNotification:themeChange postingStyle:NSPostWhenIdle];
}
显然,您将有一些 UI 来选择所需的主题。
在这种情况下,用户选择一个主题并fireTimer
0.5 秒后触发。在我强制 UI 重新绘制自身之前,这为其他 UI 的更新提供了很好的延迟。
在您需要对主题更改采取行动的任何地方收听通知。
// listen for change notification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateAppearance:) name:@"ThemeChange" object:nil];
我只有几个视图,所以我在我使用的每个控制器中编写了代码,但是您可以使用 Objective-C 的强大功能来混合代码,以更好的方式处理这个问题。
实现代码以根据主题实际重绘视图。
- (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 或类似的代码中编写代码才能在显示视图之前应用任何主题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)