本文实例为大家分享了ios主题皮肤切换代码,供大家参考,具体内容如下
1. 主题皮肤功能切换介绍
主题切换就是根据用户设置不同的主题,来动态改变用户的界面,通常会改变navigationbar背景图片、tabbar背景图片、tabbar中的按钮的图片和选中的背景图片、navigationitem.title 标题的字体颜色、ui中其他元素控件
下载源代码地址: themeskinsetup.rar
2.项目目录结构及实现效果截图
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]; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。