iphone - 将主题应用到 iPhone 应用程序的最佳方式

标签 iphone user-interface themes

嗨,我正在尝试编写一些带有主题切换器的 iPhone 应用程序,用户可以在其中选择一个主题来更改背景颜色、Alpha、图像以及一些按钮的外观和感觉(大小、图像,甚至地点)。

应用主题的最佳方式是什么?

谢谢, 蒂姆

最佳答案

以下是我如何在 FemCal 中实现更改主题的功能。我以代码片段的形式包含了一些详细信息。

  1. 创建一个单例 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;
    }

  2. 使用通知来广播主题更改。我使用@“ThemeChange”作为通知。

    - (void)fireTimer:(NSTimer *)timer
    {
    NSNotification * themeChange = [NSNotification notificationWithName:@"ThemeChange" object:nil];
    [[NSNotificationQueue defaultQueue] enqueueNotification:themeChange postingStyle:NSPostWhenIdle];
    }
    显然,您将有一些 UI 来选择所需的主题。 在本例中,用户选择一个主题并 fireTimer 0.5 秒后触发。在我强制 UI 自行重绘之前,这为其他 UI 的更新提供了很好的延迟。

  3. 在您需要对主题更改采取行动的任何地方监听通知。

    // listen for change notification
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateAppearance:) name:@"ThemeChange" object:nil];
    我只有几个 View ,因此我在我使用的每个 Controller 中编写了代码,但您可以使用 Objective-C 的强大功能来混合代码,以更好的方式处理这个问题。

  4. 实现代码以根据主题实际重绘 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/

相关文章:

ios - 如何访问父 View Controller ?

iphone - 如何在 iPhone 图片库中获取图像的嵌入式 GPS 和地理定位数据

c++ - 避免多个 bool 值的优雅方法?

winforms - 你如何处理 Winforms 中临时无用的控件(隐藏与禁用)?

java - 在 Android 中更改 Button 和 TextView 的选择颜色

iphone - 如何防止 JSONKit 从 ASP.NET JSON 日期格式转义反斜杠?

java - 使用以下代码输出错误

asp.net - 如何将多个 cssclass 名称设置为启用了 ASP.NET 主题的控件?

visual-studio-2012 - 如何更改 Visual Studio 2012 错误列表中的突出显示颜色?

iphone - FBConnect : send some images on somebody's wall