本文实例为大家分享ios自定义alertview提示框,先上图,弹框的背景色,按钮背景色,提示的消息的字体颜色都可以改变
利用单例实现丰富的自定义接口
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
|
// // pbalertcontroller.h // pbalertdemo // // created by 裴波波 on 16/4/20. // copyright © 2016年 裴波波. all rights reserved. // #import <uikit/uikit.h> typedef void (^pbblock)(); @interface pbalertcontroller : uiviewcontroller /** 设置alertview背景色 */ @property (nonatomic, copy) uicolor *alertbackgroundcolor; /** 设置确定按钮背景色 */ @property (nonatomic, copy) uicolor *btnconfirmbackgroundcolor; /** 设置取消按钮背景色 */ @property (nonatomic, copy) uicolor *btncancelbackgroundcolor; /** 设置message字体颜色 */ @property (nonatomic, copy) uicolor *messagecolor; /** 创建单例 */ +(instancetype)sharealertcontroller; /** 弹出alertview以及点击确定回调的block */ -( void )alertviewcontrollerwithmessage:(nsstring *)message andblock:(pbblock) block; @end |
.m文件中初始化控件以及对alertview的控件的属性进行懒加载,确定初始的颜色.
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
// // pbalertcontroller.m // pbalertdemo // // created by 裴波波 on 16/4/20. // copyright © 2016年 裴波波. all rights reserved. // #import "pbalertcontroller.h" /** 屏幕尺寸 */ #define kmainscreenbounds [uiscreen mainscreen].bounds @interface pbalertcontroller () /** 蒙版 */ @property (nonatomic, strong) uiview *coverview; /** 弹框 */ @property (nonatomic, strong) uiview *alertview; /** 点击确定回调的block */ @property (nonatomic, copy) pbblock block; @end @implementation pbalertcontroller - ( void )viewdidload { [super viewdidload]; self.view.backgroundcolor = [uicolor whitecolor]; } -( void )alertviewcontrollerwithmessage:(nsstring *)message andblock:(pbblock) block{ self.block = block; //创建蒙版 uiview * coverview = [[uiview alloc] initwithframe:kmainscreenbounds]; self.coverview = coverview; [self.view addsubview:coverview]; coverview.backgroundcolor = [uicolor blackcolor]; coverview.alpha = 0.7; //创建提示框view uiview * alertview = [[uiview alloc] init]; alertview.backgroundcolor = self.alertbackgroundcolor; //设置圆角半径 alertview.layer.cornerradius = 6.0; self.alertview = alertview; [self.view addsubview:alertview]; alertview.center = coverview.center; alertview.bounds = cgrectmake(0, 0, kmainscreenbounds.size.width * 0.75, kmainscreenbounds.size.width * 0.75 * 1.5/ 3); //创建操作提示 label uilabel * label = [[uilabel alloc] init]; [alertview addsubview:label]; label.text = @ "操作提示" ; label.font = [uifont systemfontofsize:19]; label.textalignment = nstextalignmentcenter; cgfloat lblwidth = alertview.bounds.size.width; cgfloat lblhigth = 22; label.frame = cgrectmake(0, 0, lblwidth, lblhigth); //创建中间灰色分割线 uiview * separateline = [[uiview alloc] init]; separateline.backgroundcolor = [uicolor graycolor]; [alertview addsubview:separateline]; separateline.frame = cgrectmake(0, lblhigth + 1, alertview.bounds.size.width, 1); //创建message label uilabel * lblmessage = [[uilabel alloc] init]; lblmessage.textcolor = self.messagecolor; [alertview addsubview:lblmessage]; lblmessage.text = message; lblmessage.textalignment = nstextalignmentcenter; lblmessage.numberoflines = 2; //最多显示两行message cgfloat margin = 5; cgfloat msgx = margin; cgfloat msgy = lblhigth + 3; cgfloat msgw = alertview.bounds.size.width - 2 * margin; cgfloat msgh = 44; lblmessage.frame = cgrectmake(msgx, msgy, msgw, msgh); //创建确定 取消按钮 cgfloat buttonwidth = (alertview.bounds.size.width - 4 * margin) * 0.5; cgfloat buttonhigth = 25; uibutton * btncancel = [[uibutton alloc] init]; [alertview addsubview:btncancel]; [btncancel settitlecolor:[uicolor whitecolor] forstate:uicontrolstatenormal]; [btncancel settitle:@ "取消" forstate:uicontrolstatenormal]; [btncancel setbackgroundcolor:self.btncancelbackgroundcolor]; btncancel.frame = cgrectmake(margin, alertview.bounds.size.height - margin - buttonhigth, buttonwidth, buttonhigth); btncancel.tag = 0; [btncancel addtarget:self action:@selector(didclickbtnconfirm:) forcontrolevents:uicontroleventtouchupinside]; //确定按钮 uibutton * btnconfirm = [[uibutton alloc] init]; btnconfirm.tag = 1; [alertview addsubview:btnconfirm]; [btnconfirm settitlecolor:[uicolor whitecolor] forstate:uicontrolstatenormal]; [btnconfirm settitle:@ "确定" forstate:uicontrolstatenormal]; [btnconfirm setbackgroundcolor:self.btnconfirmbackgroundcolor]; btnconfirm.frame = cgrectmake(alertview.bounds.size.width - margin - buttonwidth, alertview.bounds.size.height - margin - buttonhigth, buttonwidth, buttonhigth); [btnconfirm addtarget:self action:@selector(didclickbtnconfirm:) forcontrolevents:uicontroleventtouchupinside]; } /** 点击确定 or 取消触发事件 */ -( void )didclickbtnconfirm:(uibutton *)sender{ if (sender.tag == 0) { [self dismissviewcontrolleranimated:yes completion:nil]; return ; } self.block(); [self dismissviewcontrolleranimated:yes completion:nil]; } - ( void )didreceivememorywarning { [super didreceivememorywarning]; } static pbalertcontroller * instance = nil; +(instancetype)sharealertcontroller{ static dispatch_once_t oncetoken; dispatch_once(&oncetoken, ^{ instance = [[pbalertcontroller alloc] init]; }); return instance; } -(uicolor *)alertbackgroundcolor{ if (_alertbackgroundcolor == nil) { _alertbackgroundcolor = [uicolor colorwithred:249/255.0 green:249/255.0 blue:249/255.0 alpha:1]; } return _alertbackgroundcolor; } /** 确定按钮背景色 */ -(uicolor *)btnconfirmbackgroundcolor{ if (_btnconfirmbackgroundcolor == nil) { _btnconfirmbackgroundcolor = [uicolor orangecolor]; } return _btnconfirmbackgroundcolor; } /** 取消按钮背景色 */ -(uicolor *)btncancelbackgroundcolor{ if (_btncancelbackgroundcolor == nil) { _btncancelbackgroundcolor = [uicolor bluecolor]; } return _btncancelbackgroundcolor; } /** message字体颜色 */ -(uicolor *)messagecolor{ if (_messagecolor == nil) { _messagecolor = [uicolor blackcolor]; } return _messagecolor; } @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
|
// // viewcontroller.m // pbalertdemo // // created by 裴波波 on 16/4/20. // copyright © 2016年 裴波波. all rights reserved. // #import "viewcontroller.h" #import "pbalertcontroller.h" @interface viewcontroller () @end @implementation viewcontroller //点击按钮弹出提示框 - (ibaction)clickshowalertbtn:(id)sender { pbalertcontroller * alertvc = [pbalertcontroller sharealertcontroller]; alertvc.messagecolor = [uicolor redcolor]; [alertvc alertviewcontrollerwithmessage:@ "这是一message沙哈" andblock:^{ nslog(@ "点击确定后执行的方法" ); }]; alertvc.modaltransitionstyle = uimodaltransitionstylecrossdissolve; [self presentmodalviewcontroller:alertvc animated:yes]; } @end |
以上就是本文的全部内容,希望对大家学习ios程序设计有所帮助。