如何在iOS应用程序中切换皮肤(或devise主题)?

我想使我的iPhone应用程序能够在皮肤(或devise主题,或外观和感觉,如木制,金属,土色,男装,女生等)之间切换。

我将准备一些包含button和背景图像,声音和文本颜色的皮肤,并让用户决定应用程序设置使用哪组皮肤。

实施这个最好的做法是什么?

条件是:

  • 我想使用Interface Builder
  • 我需要支持iOS 3.1.3及更高版本
  • 我想从网上下载皮肤套(我不能捆绑应用程序中的所有皮肤,因为一套皮肤需要大量的图像,如果我这样做,应用程序文件的大小可能会变得巨大…我也不想硬编码有关特定皮肤的任何信息。)
  • 如果自定义皮肤不包含一个或一些元素(如图像或声音文件),我希望它使用默认皮肤集中的缺失元素。
  • 我不想为每个皮肤创buildNib文件。 一个屏幕的Nib文件应该是主包中唯一的一个,以便于维护。

我想在我的应用程序中的所有UIViewControllers的超类,并重写它加载Nib文件的一部分,而不是从主包加载,从保存在文档目录中的皮肤加载资源…但我不知道该怎么做… Nib加载方法的默认行为总是从主包中加载资源,有关资源文件名的信息在读完后会丢失… 🙁

在此先感谢您的帮助。

不知道最佳做法..但是,如果你的应用程序不够大,那么一个结构良好的plist是你的朋友。

最初,你可以select:金属主题。 以下内容应该保留:

你可以有一个Singleton ThemeManager ,或者在适当的时候把一个NSDictionary粘贴到你的一个Singleton上。

ThemeManager后面的点是资产和主题之间的映射。

一些示例代码(直接写在SOF上..不介意语法错误):

 #define kThemeMap(__x__) [[ThemeManager sharedManager] assetForCurrentTheme:__x__] ... -(void)doUselessStuff { UIImage* backgroundImage = [UIImage imageNamed:kThemeMap(@"FirstViewBG")]; ... } //in the ThemeManager: //returns the appropriate name of the asset based on current theme -(NSString*)assetForCurrentTheme:(NSString*)asset { //_currentTheme is an NSDictionary initialized from a plist. Plist can be downloaded, too. NSString* newAsset = [_currentTheme objectForKey:asset]; if(newAsset == nil) { newAsset = [_defaultTheme objectForKey:asset]; } return asset; } //Let us assume the user selects Metal Theme somewhere .. Still coding ThemeManager: -(void)selectedNewTheme:(NSString*)newTheme { //First, get the full path of the resource .. Either The main bundle, or documents directory or elsewhere.. NSString* fullPath = ...; self.currentTheme = [NSDictionary dictionaryWithContentsOfFile:fullPath]; } 

plist文件只是一个string到string映射的字典…类似这样的:

 //Default.plist @"FirstViewBG" : @"FirstViewBG_Default.png" @"SecondViewBG" : @"SecondViewBG_Default.png" @"WinSound" : @"WinSound_Default.aiff" //Metal.plist @"FirstViewBG" : @"FirstViewBG_Metal.png" @"SecondViewBG" : @"SecondViewBG_Metal.png" @"WinSound" : @"WinSound_Metal.aiff" 

或者,你可以保存后缀,如果这是足够好的..但是,这将需要string处理,通过切片扩展 – >添加后缀 – >添加扩展..

或者,也许使它成为一个前缀?

你可以使用模式化的imageNamed来对UIImage进行分类,使用自定义的imageNamed:而不是默认的。 在自定义方法中,按主题select图像。