本文實例為大家分享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程序設計有所幫助。