服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - 一步一步实现iOS主题皮肤切换效果

一步一步实现iOS主题皮肤切换效果

2021-02-03 14:40vbirdbest IOS

这篇文章主要为大家详细介绍了一步一步实现iOS主题皮肤切换效果的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了ios主题皮肤切换代码,供大家参考,具体内容如下

1. 主题皮肤功能切换介绍
主题切换就是根据用户设置不同的主题,来动态改变用户的界面,通常会改变navigationbar背景图片、tabbar背景图片、tabbar中的按钮的图片和选中的背景图片、navigationitem.title 标题的字体颜色、ui中其他元素控件

下载源代码地址: themeskinsetup.rar

2.项目目录结构及实现效果截图

一步一步实现iOS主题皮肤切换效果

一步一步实现iOS主题皮肤切换效果

一步一步实现iOS主题皮肤切换效果
 

一步一步实现iOS主题皮肤切换效果

一步一步实现iOS主题皮肤切换效果

一步一步实现iOS主题皮肤切换效果

3. 具体实现步骤

1.将image文件夹(group)和 skins拖入到项目工程中的资源文件夹中
2.创建baseviewcontroller
3.配置theme.plist
4.事项项目所需的基本框架供能,并实现主题的tableview功能
5.创建主题管理器:thememanager
6.自定义themetabbaritem 控件
7.创建ui工厂: uifactory
8. 实现tableview中的didselected事件完成主题切换
9.记录用户选择的主题,以便用户下次启动时是上次设置的主题

1.创建baseviewcontroller

?
1
2
3
4
5
#import <uikit/uikit.h>
@interface baseviewcontroller : uiviewcontroller
 
- (void) reloadthemeimage;
@end

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#import "baseviewcontroller.h"
 
#import "thememanager.h"
#import "notificationmacro.h"
 
@interface baseviewcontroller ()
 
@end
 
@implementation baseviewcontroller
- (id) init {
 if (self == [super init]) {
  [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(themechangednotfication:) name:kthemechangednotification object:nil];
 }
  
 [self reloadthemeimage];
 return self;
}
 
- (void)viewdidload {
 [super viewdidload];
 [self reloadthemeimage];
}
 
- (void)didreceivememorywarning {
 [super didreceivememorywarning];
 // dispose of any resources that can be recreated.
}
 
- (void) themechangednotfication:(nsnotification *)notification {
 [self reloadthemeimage];
}
 
- (void) reloadthemeimage {
 thememanager * thememanager = [thememanager sharedthememanager];
  
 uiimage * navigationbackgroundimage = [thememanager themeimagewithname:@"navigationbar_background.png"];
 [self.navigationcontroller.navigationbar setbackgroundimage:navigationbackgroundimage forbarmetrics:uibarmetricsdefault];
  
 uiimage * tabbarbackgroundimage = [thememanager themeimagewithname:@"tabbar_background.png"];
 [self.tabbarcontroller.tabbar setbackgroundimage:tabbarbackgroundimage];
}
@end

2. 实现appdelegate

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#import "appdelegate.h"
 
#import "mainviewcontroller.h"
#import "thememanager.h"
#import "notificationmacro.h"
 
@interface appdelegate ()
 
@end
 
@implementation appdelegate
 
 
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions {
 [self inituserdefaultconfig];
  
 mainviewcontroller * rootviewcontroller = [[mainviewcontroller alloc] init];
 self.window.rootviewcontroller = rootviewcontroller;
  
 return yes;
}
 
 
- (void) inituserdefaultconfig {
 nsstring * themename = [[nsuserdefaults standarduserdefaults] objectforkey:kthemenamekey];
 thememanager * thememanager = [thememanager sharedthememanager];
 thememanager.themename = themename;
}</span></span>

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<span style="font-weight: normal;"><span style="font-weight: normal;">#import "mainviewcontroller.h"
 
#import "homeviewcontroller.h"
#import "messageviewcontroller.h"
#import "mineviewcontroller.h"
 
#import "uifactory.h"
 
 
@interface mainviewcontroller ()
 
@end
 
@implementation mainviewcontroller
 
- (id) init {
 if (self = [super init]) {
  [self inittabbarui];
 }
  
 return self;
}
 
- (void)viewdidload {
 [super viewdidload];
  
}
 
- (void)didreceivememorywarning {
 [super didreceivememorywarning];
}
 
 
- (void) inittabbarui {
 // 主页
 homeviewcontroller * homeviewcontroller = [[homeviewcontroller alloc] init];
 uinavigationcontroller * homenavigationcontroller = [[uinavigationcontroller alloc] initwithrootviewcontroller:homeviewcontroller];
// uitabbaritem * hometabbaritem = [[uitabbaritem alloc] initwithtitle:@"主页" image:[uiimage imagenamed:@"tabbar_home"] selectedimage:[uiimage imagenamed:@"tabbar_home_selected"]];
 uitabbaritem * hometabbaritem = [uifactory createtabbaritemwithtitle:@"主页" imagename:@"tabbar_home" selectedimage:@"tabbar_home_selected"];
 homenavigationcontroller.tabbaritem = hometabbaritem;
  
 // 消息(中心)
 messageviewcontroller * messageviewcontroller = [[messageviewcontroller alloc] init];
 uinavigationcontroller * messagenavigationcontroller = [[uinavigationcontroller alloc] initwithrootviewcontroller:messageviewcontroller];
// uitabbaritem * messagetabbaritem = [[uitabbaritem alloc] initwithtitle:@"消息" image:[uiimage imagenamed:@"tabbar_message_center"] selectedimage:[uiimage imagenamed:@"tabbar_message_center_selected"]];
 uitabbaritem * messagetabbaritem = [uifactory createtabbaritemwithtitle:@"消息" imagename:@"tabbar_message_center" selectedimage:@"tabbar_message_center_selected"];
 messagenavigationcontroller.tabbaritem = messagetabbaritem;
  
 // 我
 mineviewcontroller * mineviewcontroller = [[mineviewcontroller alloc] init];
 uinavigationcontroller * minenavigationcontroller = [[uinavigationcontroller alloc] initwithrootviewcontroller:mineviewcontroller];
// uitabbaritem * minetabbaritem = [[uitabbaritem alloc] initwithtitle:@"我" image:[uiimage imagenamed:@"tabbar_profile"] selectedimage:[uiimage imagenamed:@"tabbar_profile_selected"]];
 uitabbaritem * minetabbaritem = [uifactory createtabbaritemwithtitle:@"我" imagename:@"tabbar_profile" selectedimage:@"tabbar_profile_selected"];
  
  
 minenavigationcontroller.tabbaritem = minetabbaritem;
 nsarray * viewcontrollers = @[homenavigationcontroller, messagenavigationcontroller, minenavigationcontroller];
 self.viewcontrollers = viewcontrollers;
}
 
 
@end

3. 创建主题管理器

?
1
2
3
4
5
6
7
8
9
10
11
12
#import <foundation/foundation.h>
#import <uikit/uikit.h>
 
@interface thememanager : nsobject
 
@property (nonatomic, copy) nsstring * themename;   // 主题名字
@property (nonatomic, retain) nsdictionary * themeplistdict; // 主题属性列表字典
 
+ (thememanager *) sharedthememanager;
 
- (uiimage *) themeimagewithname:(nsstring *)imagename;
@end</span></span>

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<span style="font-weight: normal;"><span style="font-weight: normal;">#import <foundation/foundation.h>
#import <uikit/uikit.h>
 
@interface thememanager : nsobject
 
@property (nonatomic, copy) nsstring * themename;   // 主题名字
@property (nonatomic, retain) nsdictionary * themeplistdict; // 主题属性列表字典
 
+ (thememanager *) sharedthememanager;
 
- (uiimage *) themeimagewithname:(nsstring *)imagename;
@end
#import "thememanager.h"
#import "notificationmacro.h"
static thememanager * sharedthememanager;
 
@implementation thememanager
 
- (id) init {
 if(self = [super init]) {
  nsstring * themepath = [[nsbundle mainbundle] pathforresource:@"theme" oftype:@"plist"];
  self.themeplistdict = [nsdictionary dictionarywithcontentsoffile:themepath];
  self.themename = nil;
 }
  
 return self;
}
 
+ (thememanager *) sharedthememanager {
 @synchronized(self) {
  if (nil == sharedthememanager) {
   sharedthememanager = [[thememanager alloc] init];
  }
 }
  
 return sharedthememanager;
}
 
// override 重写themename的set方法
- (void) setthemename:(nsstring *)themename {
 _themename = themename;
}
 
- (uiimage *) themeimagewithname:(nsstring *)imagename {
 if (imagename == nil) {
  return nil;
 }
  
 nsstring * themepath = [self themepath];
 nsstring * themeimagepath = [themepath stringbyappendingpathcomponent:imagename];
 uiimage * themeimage = [uiimage imagewithcontentsoffile:themeimagepath];
  
 return themeimage;
}
 
// 返回主题路径
- (nsstring *)themepath {
 nsstring * resourcepath = [[nsbundle mainbundle] resourcepath];
 if (self.themename == nil || [self.themename isequaltostring:@""]) {
  return resourcepath;
 }
  
  
 nsstring * themesubpath = [self.themeplistdict objectforkey:self.themename]; // skins/blue
 nsstring * themefilepath = [resourcepath stringbyappendingpathcomponent:themesubpath]; // .../skins/blue
  
 return themefilepath;
}
@end

4. 创建主题按钮 themetabbaritem

?
1
2
3
4
5
6
7
8
9
10
#import <uikit/uikit.h>
@interface themetabbaritem : uitabbaritem
 
@property (nonatomic, copy) nsstring * imagename;
@property (nonatomic, copy) nsstring * selectedimagename;
 
 
- (id) initwithtitle:(nsstring *)title imagename:(nsstring *)imagename selectedimage:(nsstring *)selectedimagename;
 
@end </span></span>

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<span style="font-weight: normal;"><span style="font-weight: normal;">#import "themetabbaritem.h"
#import "thememanager.h"
#import "notificationmacro.h"
 
@implementation themetabbaritem
 
// 初始化时注册观察者
- (id) init {
 if (self = [super init]) {
  [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(themechangednotification:) name:kthemechangednotification object:nil];
 }
  
 return self;
}
 
- (id) initwithtitle:(nsstring *)title imagename:(nsstring *)imagename selectedimage:(nsstring *)selectedimagename {
 if (self = [self init]) {
  self.title = title;
  self.imagename = imagename;   // 此时会调用[self setimagename:imagename] ---> [self reloadthemeimage] --->[self setimage:image]
  self.selectedimagename = selectedimagename;// 此时会调用[self setselectedimagename:selectedimagename];
 }
  
 return self;
}
 
 
#pragma mark -
#pragma mark - override setter
- (void) setimagename:(nsstring *)imagename {
 if (_imagename != imagename) {
  _imagename = imagename;
 }
  
 [self reloadthemeimage];
}
 
- (void) setselectedimagename:(nsstring *)selectedimagename {
 if (_selectedimagename != selectedimagename) {
  _selectedimagename = selectedimagename;
 }
  
 [self reloadthemeimage];
}
 
 
 
// 主题改变之后重新加载图片
- (void)themechangednotification:(nsnotification *)notification {
 [self reloadthemeimage];
}
 
- (void)reloadthemeimage {
 thememanager * thememanager = [thememanager sharedthememanager];
  
 if (self.imagename != nil) {
  uiimage * image = [thememanager themeimagewithname:self.imagename];
  [self setimage:image];
 }
  
 if (self.selectedimagename != nil) {
  uiimage * selectedimage = [thememanager themeimagewithname:self.selectedimagename];
  [self setselectedimage:selectedimage];
 }
}
 
- (void) dealloc {
 [[nsnotificationcenter defaultcenter] removeobserver:self];
}

5. 创建ui工厂

?
1
2
3
4
5
6
7
8
9
#import <foundation/foundation.h>
#import <uikit/uikit.h>
 
@interface uifactory : nsobject
 
+ (uitabbaritem *) createtabbaritemwithtitle:(nsstring *)title imagename:(nsstring *)imagename selectedimage:(nsstring *)selectedimagename;
 
 
@end</span></span>
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<span style="font-weight: normal;"><span style="font-weight: normal;">#import <foundation/foundation.h>
#import <uikit/uikit.h>
 
@interface uifactory : nsobject
 
+ (uitabbaritem *) createtabbaritemwithtitle:(nsstring *)title imagename:(nsstring *)imagename selectedimage:(nsstring *)selectedimagename;
 
 
@end
#import "uifactory.h"
 
#import "themetabbaritem.h"
@implementation uifactory
 
+ (uitabbaritem *) createtabbaritemwithtitle:(nsstring *)title imagename:(nsstring *)imagename selectedimage:(nsstring *)selectedimagename {
 themetabbaritem * themetabbaritem = [[themetabbaritem alloc] initwithtitle:title imagename:imagename selectedimage:selectedimagename];
  
 return themetabbaritem;
}
@end

6. 实现选中单元格的事件

?
1
2
3
4
5
6
7
8
9
#import "baseviewcontroller.h"
 
@interface mineviewcontroller : baseviewcontroller <uitableviewdelegate, uitableviewdatasource>
 
 
@property (weak, nonatomic) iboutlet uitableview *tableview;
 
@property (nonatomic, retain) nsmutablearray * themedatasource;
@end

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#import "baseviewcontroller.h"
 
@interface mineviewcontroller : baseviewcontroller <uitableviewdelegate, uitableviewdatasource>
 
 
@property (weak, nonatomic) iboutlet uitableview *tableview;
 
@property (nonatomic, retain) nsmutablearray * themedatasource;
@end
#import "mineviewcontroller.h"
 
#import "thememanager.h"
#import "notificationmacro.h"
 
@interface mineviewcontroller ()
 
@end
 
@implementation mineviewcontroller
 
- (void)viewdidload {
 [super viewdidload];
 self.title = @"我";
  
 thememanager * thememanager = [thememanager sharedthememanager];
 _themedatasource = [nsmutablearray arraywitharray:thememanager.themeplistdict.allkeys];
}
 
- (void)didreceivememorywarning {
 [super didreceivememorywarning];
 // dispose of any resources that can be recreated.
}
 
 
 
#pragma mark -
#pragma mark - uitableviewdelegate
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section {
 
 return self.themedatasource.count;
}
 
- (uitableviewcell *) tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath {
 static nsstring * identifier = @"cell";
 uitableviewcell * cell = [tableview dequeuereusablecellwithidentifier:identifier];
 if (cell == nil) {
  cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:identifier];
 }
  
 nsstring * text = self.themedatasource[indexpath.row];
 cell.textlabel.text = text;
  
 thememanager * thememanager = [thememanager sharedthememanager];
 nsstring * currenttheme = thememanager.themename;
 if (currenttheme == nil) {
  currenttheme = @"默认";
 }
 if ([currenttheme isequaltostring:text]) {
  cell.accessorytype = uitableviewcellaccessorycheckmark;
 } else {
  cell.accessorytype = uitableviewcellaccessorynone;
 }
  
 return cell;
}
 
- (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)indexpath {
  
 thememanager * thememanager = [thememanager sharedthememanager];
 nsstring * themename = self.themedatasource[indexpath.row];
  
 if ([themename isequaltostring:@"默认"]) {
  themename = nil;
 }
  
 // 记录当前主题名字
 thememanager.themename = themename;
 [[nsnotificationcenter defaultcenter] postnotificationname:kthemechangednotification object:nil];
  
  
 // 主题持久化
 nsuserdefaults * userdefaults = [nsuserdefaults standarduserdefaults];
 [userdefaults setobject:themename forkey:kthemenamekey];
 [userdefaults synchronize];
  
 // 重新加载数据显示uitableviewcellaccessorycheckmark 显示选中的对号 v
 [self.tableview reloaddata];
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17