ios自定义uidatepicker日期选择器视图 ,首先看一下效果图:
下面贴上相关代码:
viewcontroller:
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
|
#import <uikit/uikit.h> @interface viewcontroller : uiviewcontroller @end #import "viewcontroller.h" #import "hwdatepicker.h" #define mainw [uiscreen mainscreen].bounds.size.width #define mainh [uiscreen mainscreen].bounds.size.height @interface viewcontroller ()<uitextfielddelegate, hwdatepickerdelegate> @property (nonatomic, weak) hwdatepicker *datepicker; @property (nonatomic, strong) uitextfield *datetextfield; @end @implementation viewcontroller - ( void )viewdidload { [super viewdidload]; self.view.backgroundcolor = [uicolor blackcolor]; //创建控件 [self creatcontrol]; } - ( void )creatcontrol { //textfield _datetextfield = [[uitextfield alloc] initwithframe:cgrectmake(mainw * 0.05, mainw * 0.72, mainw * 0.9, mainw * 0.12)]; _datetextfield.background = [uiimage imagenamed:@ "textfieldbj" ]; _datetextfield.textalignment = nstextalignmentright; _datetextfield.placeholder = @ "请设置日期" ; _datetextfield.delegate = self; uilabel *lab2 = [[uilabel alloc] initwithframe:cgrectmake(0, 0, mainw * 0.4, mainw * 0.12)]; lab2.textalignment = nstextalignmentleft; lab2.text = @ " 日期" ; lab2.textcolor = [uicolor graycolor]; _datetextfield.leftview = lab2; _datetextfield.leftviewmode = uitextfieldviewmodealways; uilabel *lab22 = [[uilabel alloc] initwithframe:cgrectmake(mainw * 0.12 - 15, 0, 15, mainw * 0.12)]; _datetextfield.rightview = lab22; _datetextfield.rightviewmode = uitextfieldviewmodealways; [self.view addsubview:_datetextfield]; //日期选择器 hwdatepicker *datepicker = [[hwdatepicker alloc] initwithframe:cgrectmake(mainw * 0.05, mainh, mainw * 0.9, mainw * 0.5)]; datepicker.delegate = self; [self.view addsubview:datepicker]; self.datepicker = datepicker; } #pragma mark - uitextfielddelegate - ( bool )textfieldshouldbeginediting:(uitextfield *)textfield { if (_datepicker.frame.origin.y != mainh && _datepicker != nil) { [_datepicker dismiss]; return no; } else if (textfield == _datetextfield) { [_datepicker show]; return no; } return yes; } #pragma mark - hwdatepickerdelegate - ( void )datepickerview:(hwdatepicker *)datepickerview didclicksurebtnwithselectdate:(nsstring *)date { _datetextfield.text = date; } @end |
hwdatepicker:
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
|
#import <uikit/uikit.h> @ class hwdatepicker; @protocol hwdatepickerdelegate <nsobject> /** * hwdatepicker确定按钮点击代理事件 * * @param datepickerview hwdatepicker * @param date 选中的日期 */ - ( void )datepickerview:(hwdatepicker *)datepickerview didclicksurebtnwithselectdate:(nsstring *)date; @end @interface hwdatepicker : uiview @property (nonatomic, weak) id<hwdatepickerdelegate> delegate; - ( void )show; - ( void )dismiss; @end #import "hwdatepicker.h" //获得屏幕的宽高 #define mainw [uiscreen mainscreen].bounds.size.width #define mainh [uiscreen mainscreen].bounds.size.height @interface hwdatepicker () @property (nonatomic, strong) uidatepicker *datepicker; @end @implementation hwdatepicker - (id)initwithframe:(cgrect)frame { if (self = [super initwithframe:frame]) { //背景框 uiimageview *back = [[uiimageview alloc] initwithframe:cgrectmake(0, 0, self.bounds.size.width, self.bounds.size.height)]; back.image = [uiimage imagenamed:@ "datepickerbj" ]; [self addsubview:back]; //日期选择器 _datepicker = [[uidatepicker alloc] init]; _datepicker.frame = cgrectmake(10, 10, self.frame.size.width - 20, 120); _datepicker.backgroundcolor = [uicolor clearcolor]; [_datepicker setdatepickermode:uidatepickermodedate]; nslocale *locale = [[nslocale alloc] initwithlocaleidentifier:@ "zh_cn" ]; _datepicker.locale = locale; nsdateformatter *formatter_mindate = [[nsdateformatter alloc] init]; [formatter_mindate setdateformat:@ "yyyy-mm-dd" ]; nsdate *mindate = [formatter_mindate datefromstring:@ "2008-01-01" ]; formatter_mindate = nil; [_datepicker setminimumdate:mindate]; [self addsubview:_datepicker]; //确定按钮 uibutton *surebtn = [[uibutton alloc] initwithframe:cgrectmake((self.frame.size.width - mainw * 0.36) * 0.5, self.frame.size.height * 0.747, mainw * 0.36, mainw * 0.11)]; [surebtn setimage:[uiimage imagenamed:@ "surebtn" ] forstate:uicontrolstatenormal]; [surebtn addtarget:self action:@selector(surebtnonclick) forcontrolevents:uicontroleventtouchupinside]; [self addsubview:surebtn]; } return self; } - ( void )surebtnonclick { [self dismiss]; if (_delegate && [_delegate respondstoselector:@selector(datepickerview:didclicksurebtnwithselectdate:)]) { [_delegate datepickerview:self didclicksurebtnwithselectdate:[self getdatestring]]; } } - (nsstring *)getdatestring { nsdateformatter *dateformatter = [[nsdateformatter alloc] init]; [dateformatter setdateformat:@ "yyyy-mm-dd" ]; nsstring *date = [dateformatter stringfromdate:[self.datepicker date]]; return date; } - ( void )show { [uiview animatewithduration:0.3 animations:^{ self.frame = cgrectmake(mainw * 0.05, mainh - mainw * 0.75, mainw * 0.9, mainw * 0.5); }]; } - ( void )dismiss { [uiview animatewithduration:0.3 animations:^{ self.frame = cgrectmake(mainw * 0.05, mainh, mainw * 0.9, mainw * 0.5); }]; } @end |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/hero_wqb/article/details/51277512