iOS自定义UIDatePicker日期选择器视图
程序员文章站
2023-11-30 08:16:58
ios自定义uidatepicker日期选择器视图 ,首先看一下效果图:
下面贴上相关代码:
viewcontroller:
#import
ios自定义uidatepicker日期选择器视图 ,首先看一下效果图:
下面贴上相关代码:
viewcontroller:
#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:
#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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读