您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
由于项目需要,需要定制一个日期选择器,找了半天没找到合适的就自己写了个demo
自定义UIDatePicker日期选择器视图
效果如下:
下面贴上相关代码:
ViewController:
<pre name="code" class="objc">- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.view.backgroundColor = [UIColor whiteColor]; if (!_picker || _picker == nil) { _picker = [[DatePicker alloc] initWithFrame:CGRectMake(0, 120, 375, 667)]; _picker.backgroundColor = [UIColor whiteColor]; _picker.delegate = self; [self.view addSubview:_picker]; _picker.hidden = YES; } [self creatCustomButton]; } - (void)creatCustomButton{ UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(120, 20, 100, 40); [button setTitle:@"日期选择器" forState:UIControlStateNormal]; [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside ]; [self.view addSubview:button]; } - (void)buttonAction:(UIButton *)sender { if (!_picker || _picker == nil) { _picker = [[DatePicker alloc] initWithFrame:CGRectMake(0, 120, 375, 667)]; _picker.backgroundColor = [UIColor whiteColor]; _picker.delegate = self; [self.view addSubview:_picker]; }else{ _picker.hidden = NO; } } -(void)DatePickerDelegateEnterActionWithDataPicker:(DatePicker *)picker{ NSLog(@"======>%@",picker.date); NSString *dateStr = [NSString stringWithFormat:@"%@",picker.date]; [_picker removeFromSuperview]; _picker = nil; [[[UIAlertView alloc]initWithTitle:@"提示" message:dateStr delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil] show]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end</pre><br> <pre></pre> <br> DatePicker.h<br> <p><span ></span></p> <pre code_snippet_id="2093925" snippet_file_name="blog_20170103_2_3524896" name="code" class="objc">#import <UIKit/UIKit.h> @protocol DatePickerDelegate; @interface DatePicker : UIView <UIPickerViewDataSource,UIPickerViewDelegate> // 按照规范, 请把这些设置为外部不可见的 // 不可见的部分, 请放到.m文件里 @property (nonatomic, retain) UIPickerView* yearPicker; // 年 @property (nonatomic, retain) UIPickerView* monthPicker; // 月 @property (nonatomic, retain) UIPickerView* dayPicker; // 日 @property (nonatomic, retain) UIPickerView* hourPicker; // 时 @property (nonatomic, retain) UIPickerView* minutePicker; // 分 // 对外可见的 @property (nonatomic, retain) NSDate* date; // 当前date // 不可见的 @property (nonatomic, retain) UIToolbar* toolBar; // 工具条 @property (nonatomic, retain) UILabel* hintsLabel; // 提示信息 // 不可见的 @property (nonatomic, retain) NSMutableArray* yearArray; @property (nonatomic, retain) NSMutableArray* monthArray; @property (nonatomic, retain) NSMutableArray* dayArray; @property (nonatomic, retain) NSMutableArray* hourArray; @property (nonatomic, retain) NSMutableArray* minuteArray; // 不可见的 @property (nonatomic, assign) NSUInteger yearValue; @property (nonatomic, assign) NSUInteger monthValue; @property (nonatomic, assign) NSUInteger dayValue; @property (nonatomic, assign) NSUInteger hourValue; @property (nonatomic, assign) NSUInteger minuteValue; @property (nonatomic, copy) NSString *dateString; /** * 设置默认值为当前时间 */ -(void)resetDateToCurrentDate; /** * 设置提示信息 */ -(void)setHintsText:(NSString*)hints; /** * 点击确定按钮 // 按照习惯,这个可木有 */ -(IBAction)actionEnter:(id)sender; @property (nonatomic, assign) id<DatePickerDelegate>delegate; @end @protocol DatePickerDelegate <NSObject> /** * 点击确定后的事件 */ @optional -(void)DatePickerDelegateEnterActionWithDataPicker:(DatePicker*)picker; @end </pre><br> <p></p> <p class="p1">Datepicker.m<span class="s1"></span></p> <p class="p1"></p> <pre code_snippet_id="2093925" snippet_file_name="blog_20170103_3_4070113" name="code" class="objc"><pre code_snippet_id="2093925" snippet_file_name="blog_20170103_3_4070113" name="code" class="objc">#import "DatePicker.h" #import "NSDate+Calendar.h" #define MainHeight [UIScreen mainScreen].bounds.size.height #define MainWidth [UIScreen mainScreen].bounds.size.width typedef enum { ePickerViewTagYear = 2012, ePickerViewTagMonth, ePickerViewTagDay, ePickerViewTagHour, ePickerViewTagMinute }PickViewTag; @interface DatePicker (private) /** * 创建数据源 */ -(void)createDataSource; /** * create month Arrays */ -(void)createMonthArrayWithYear:(NSInteger)yearInt month:(NSInteger)monthInt; @end @implementation DatePicker @synthesize delegate; @synthesize yearPicker, monthPicker, dayPicker, hourPicker, minutePicker; @synthesize date; @synthesize yearArray, monthArray, dayArray, hourArray, minuteArray; @synthesize toolBar, hintsLabel; @synthesize yearValue, monthValue; @synthesize dayValue, hourValue, minuteValue; #pragma mark - #pragma mark - - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:CGRectMake(0, MainHeight - 260, MainWidth, 260)]; if (self) { // Initialization code [self setBackgroundColor:[UIColor blackColor]]; NSMutableArray* tempArray1 = [[NSMutableArray alloc] initWithCapacity:0]; NSMutableArray* tempArray2 = [[NSMutableArray alloc] initWithCapacity:0]; NSMutableArray* tempArray3 = [[NSMutableArray alloc] initWithCapacity:0]; NSMutableArray* tempArray4 = [[NSMutableArray alloc] initWithCapacity:0]; NSMutableArray* tempArray5 = [[NSMutableArray alloc] initWithCapacity:0]; [self setYearArray:tempArray1]; [self setMonthArray:tempArray2]; [self setDayArray:tempArray3]; [self setHourArray:tempArray4]; [self setMinuteArray:tempArray5]; // 更新数据源 [self createDataSource]; CGFloat pRate = MainWidth/320; // 创建 toolBar & hintsLabel & enter button UIToolbar* tempToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, MainWidth, 44)]; [self setToolBar:tempToolBar]; [self addSubview:self.toolBar]; // [toolBar setTintColor:[UIColor lightTextColor]]; UILabel* tempHintsLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 250, 34)]; [self setHintsLabel:tempHintsLabel]; [self.hintsLabel setBackgroundColor:[UIColor clearColor]]; [self addSubview:self.hintsLabel]; [self.hintsLabel setFont:[UIFont systemFontOfSize:24.0f]]; [self.hintsLabel setTextColor:[UIColor whiteColor]]; UIButton* tempBtn = [UIButton buttonWithType:UIButtonTypeCustom]; // [tempBtn setBackgroundImage:[UIImage imageNamed:@"Resourse.bundle/btnNormal.png"] forState:UIControlStateNormal]; // [tempBtn setBackgroundImage:[UIImage imageNamed:@"Resourse.bundle/btnPressed.png"] forState:UIControlStateNormal]; [tempBtn setTitle:@"确定" forState:UIControlStateNormal]; [tempBtn sizeToFit]; [tempBtn setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal]; [tempBtn setCenter:CGPointMake(MainWidth-15-tempBtn.frame.size.width*.5, 22)]; [tempBtn addTarget:self action:@selector(actionEnter:) forControlEvents:UIControlEventTouchUpInside]; [self addSubview:tempBtn]; // 初始化各个视图 UIPickerView* yearPickerTemp = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 44, 80, 216)]; [self setYearPicker:yearPickerTemp]; [self.yearPicker setFrame:CGRectMake(0*pRate, 44, 80*pRate, 216)]; // UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake( 75*pRate, 141 ,20, 20)]; // label.font = [UIFont systemFontOfSize:18]; // label.text = @"年"; // [self addSubview:label]; UIPickerView* monthPickerTemp = [[UIPickerView alloc] initWithFrame:CGRectMake(80, 44, 56*pRate, 216)]; [self setMonthPicker:monthPickerTemp]; [self.monthPicker setFrame:CGRectMake(80*pRate, 44, 56*pRate, 216)]; UIPickerView* dayPickerTemp = [[UIPickerView alloc] initWithFrame:CGRectMake(141, 44, 60, 216)]; [self setDayPicker:dayPickerTemp]; [self.dayPicker setFrame:CGRectMake(126*pRate, 44, 60*pRate, 216)]; UIPickerView* hourPickerTemp = [[UIPickerView alloc] initWithFrame:CGRectMake(201, 44, 60, 216)]; [self setHourPicker:hourPickerTemp]; [self.hourPicker setFrame:CGRectMake(186*pRate, 44, 60*pRate, 216)]; UIPickerView* minutesPickerTemp = [[UIPickerView alloc] initWithFrame:CGRectMake(261, 44, 60, 216)]; [self setMinutePicker:minutesPickerTemp]; [self.minutePicker setFrame:CGRectMake(246*pRate, 44, 60*pRate, 216)]; NSArray *arrar = @[@"年",@"月",@"日",@"时",@"分"]; UILabel *NameLabel; for (int i = 0; i < 5; i++) { switch (i) { case 0: { NameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 75*pRate, 141 ,20, 20)]; break; } case 1: { NameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 126*pRate, 141 ,20, 20)]; break; } case 2: { NameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 186*pRate, 141 ,20, 20)]; break; } case 3: { NameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 246*pRate, 141 ,20, 20)]; break; } case 4: { NameLabel = [[UILabel alloc] initWithFrame:CGRectMake( 296*pRate, 141 ,20, 20)]; break; } default: break; } NameLabel.font = [UIFont systemFontOfSize:18]; NameLabel.text = [NSString stringWithFormat:@"%@",arrar[i]]; [self addSubview:NameLabel]; } [self.yearPicker setDataSource:self]; [self.monthPicker setDataSource:self]; [self.dayPicker setDataSource:self]; [self.hourPicker setDataSource:self]; [self.minutePicker setDataSource:self]; [self.yearPicker setDelegate:self]; [self.monthPicker setDelegate:self]; [self.dayPicker setDelegate:self]; [self.hourPicker setDelegate:self]; [self.minutePicker setDelegate:self]; [self.yearPicker setTag:ePickerViewTagYear]; [self.monthPicker setTag:ePickerViewTagMonth]; [self.dayPicker setTag:ePickerViewTagDay]; [self.hourPicker setTag:ePickerViewTagHour]; [self.minutePicker setTag:ePickerViewTagMinute]; [self addSubview:self.yearPicker]; [self addSubview:self.monthPicker]; [self addSubview:self.dayPicker]; [self addSubview:self.hourPicker]; [self addSubview:self.minutePicker]; [self.yearPicker setShowsSelectionIndicator:YES]; [self.monthPicker setShowsSelectionIndicator:YES]; [self.dayPicker setShowsSelectionIndicator:YES]; [self.hourPicker setShowsSelectionIndicator:YES]; [self.minutePicker setShowsSelectionIndicator:YES]; [self resetDateToCurrentDate]; } return self; } #pragma mark - UIPickerViewDataSource - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1; } //- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ // if (ePickerViewTagYear == pickerView.tag) { // return 60.0f; // } else { // return 40.0f; // } //} -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if (ePickerViewTagYear == pickerView.tag) { return [self.yearArray count]; } if (ePickerViewTagMonth == pickerView.tag) { return [self.monthArray count]; } if (ePickerViewTagDay == pickerView.tag) { return [self.dayArray count]; } if (ePickerViewTagHour == pickerView.tag) { return [self.hourArray count]; } if (ePickerViewTagMinute == pickerView.tag) { return [self.minuteArray count]; } return 0; } #pragma makr - UIPickerViewDelegate - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if (ePickerViewTagYear == pickerView.tag) { return [self.yearArray objectAtIndex:row]; } if (ePickerViewTagMonth == pickerView.tag) { return [self.monthArray objectAtIndex:row]; } if (ePickerViewTagDay == pickerView.tag) { return [self.dayArray objectAtIndex:row]; } if (ePickerViewTagHour == pickerView.tag) { return [self.hourArray objectAtIndex:row]; } if (ePickerViewTagMinute == pickerView.tag) { return [self.minuteArray objectAtIndex:row]; } return @""; } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (ePickerViewTagYear == pickerView.tag) { self.yearValue = [[self.yearArray objectAtIndex:row] intValue]; } else if(ePickerViewTagMonth == pickerView.tag){ self.monthValue = [[self.monthArray objectAtIndex:row] intValue]; } else if(ePickerViewTagDay == pickerView.tag){ self.dayValue = [[self.dayArray objectAtIndex:row]intValue]; } else if(ePickerViewTagHour == pickerView.tag){ self.hourValue = [[self.hourArray objectAtIndex:row]intValue]; } else if(ePickerViewTagMinute == pickerView.tag){ self.minuteValue = [[self.minuteArray objectAtIndex:row] intValue]; } if (ePickerViewTagMonth == pickerView.tag || ePickerViewTagYear == pickerView.tag) { [self createMonthArrayWithYear:self.yearValue month:self.monthValue]; [self.dayPicker reloadAllComponents]; } NSString* str = [NSString stringWithFormat:@"%04d%02d%02lu%02lu%02lu",self.yearValue, self.monthValue, (unsigned long)self.dayValue, (unsigned long)self.hourValue, (unsigned long)self.minuteValue]; [self setDate:[self dateFromString:str withFormat:@"yyyyMMddHHmm"]]; } #pragma mark - 年月日闰年=情况分析 /** * 创建数据源 */ -(void)createDataSource{ // 年 NSInteger yearInt = [[NSDate date] getYear]; [self.yearArray removeAllObjects]; for (int i=yearInt ; i<= yearInt + 10; i++) { [self.yearArray addObject:[NSString stringWithFormat:@"%d",i]]; } // 月 [self.monthArray removeAllObjects]; for (int i=1; i<=12; i++) { [self.monthArray addObject:[NSString stringWithFormat:@"%d",i]]; } NSInteger month = [[NSDate date] getMonth]; [self createMonthArrayWithYear:yearInt month:month]; // 时 [self.hourArray removeAllObjects]; for(int i=0; i<24; i++){ [self.hourArray addObject:[NSString stringWithFormat:@"%02d",i]]; } // 分 [self.minuteArray removeAllObjects]; for(int i=0; i<60; i++){ [self.minuteArray addObject:[NSString stringWithFormat:@"%02d",i]]; } } #pragma mark - -(void)resetDateToCurrentDate{ NSDate* nowDate = [NSDate date]; [self.yearPicker selectRow:0 inComponent:0 animated:YES]; [self.monthPicker selectRow:[nowDate getMonth]-1 inComponent:0 animated:YES]; [self.dayPicker selectRow:[nowDate getDay]-1 inComponent:0 animated:YES]; [self.hourPicker selectRow:[nowDate getHour] inComponent:0 animated:YES]; [self.minutePicker selectRow:[nowDate getMinute] inComponent:0 animated:YES]; [self setYearValue:[nowDate getYear]]; [self setMonthValue:[nowDate getMonth]]; [self setDayValue:[nowDate getDay]]; [self setHourValue:[nowDate getHour]]; [self setMinuteValue:[nowDate getMinute]]; NSString* str = [NSString stringWithFormat:@"%04d%02d%02d%02d%02d",self.yearValue, self.monthValue, self.dayValue, self.hourValue, self.minuteValue]; [self setDate:[self dateFromString:str withFormat:@"yyyyMMddHHmm"]]; self.dateString = [self dateString:self.date]; } #pragma mark - -(void)createMonthArrayWithYear:(NSInteger)yearInt month:(NSInteger)monthInt{ int endDate = 0; switch (monthInt) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: endDate = 31; break; case 4: case 6: case 9: case 11: endDate = 30; break; case 2: // 是否为闰年 if (yearInt % 400 == 0) { endDate = 29; } else { if (yearInt % 100 != 0 && yearInt %4 ==4) { endDate = 29; } else { endDate = 28; } } break; default: break; } if (self.dayValue > endDate) { self.dayValue = endDate; } // 日 [self.dayArray removeAllObjects]; for(int i=1; i<=endDate; i++){ [self.dayArray addObject:[NSString stringWithFormat:@"%d",i]]; } } #pragma mark - 点击确定按钮 -(void)actionEnter:(id)sender{ if (self.date == nil) { self.date =[NSDate date]; } NSLog(@"====>%@",self.date); if (self.delegate && [self.delegate respondsToSelector:@selector(DatePickerDelegateEnterActionWithDataPicker:)]) { [self.delegate DatePickerDelegateEnterActionWithDataPicker:self]; } [self removeFromSuperview]; } #pragma mark - 设置提示信息 -(void)setHintsText:(NSString*)hints{ [self.hintsLabel setText:hints]; } #pragma mark - -(void)removeFromSuperview{ self.delegate = nil; [super removeFromSuperview]; } - (NSDate *)dateFromString:(NSString *)str withFormat:(NSString *)format{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; [dateFormatter setDateFormat:format]; NSDate* dateFromString = [dateFormatter dateFromString:str]; return dateFromString; } - (NSString *)dateString:(NSDate *)date{ NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; [dateFormatter setDateFormat:@"yyyyy-MM-dd HH:mm:ss"]; NSString *dateString = [dateFormatter stringFromDate:date]; return dateString; } @end </pre><br> <pre></pre> <br> NSDate+Calendar.h<br> <p></p> <p class="p1"></p> <pre code_snippet_id="2093925" snippet_file_name="blog_20170103_4_4657975" name="code" class="objc">#import <Foundation/Foundation.h> //头文件部分 @interface NSDate (Calendar) /** *获取当前月的天数 */ - (NSUInteger)YHBaseNumberOfDaysInCurrentMonth; /** *获取本月第一天 */ - (NSDate *)YHBaseFirstDayOfCurrentMonth; //下面这些方法用于获取各种整形的数据 /** *确定某天是周几 */ -(int)YHBaseWeekly; /** *年月日 时分秒 */ -(int)getYear; -(int)getMonth; -(int)getDay; -(int)getHour; -(int)getMinute; -(int)getSecond; @end </pre> <p></p> <p class="p1"><br> </p> <p class="p1"><br> </p> NSDate+Calendar.m <p class="p1"></p> <pre code_snippet_id="2093925" snippet_file_name="blog_20170103_5_503109" name="code" class="objc">#import "NSDate+Calendar.h" @implementation NSDate (Calendar) -(NSUInteger)YHBaseNumberOfDaysInCurrentMonth{ return [[NSCalendar currentCalendar] rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:self].length; } - (NSDate *)YHBaseFirstDayOfCurrentMonth { NSDate *startDate = nil; BOOL ok = [[NSCalendar currentCalendar] rangeOfUnit:NSMonthCalendarUnit startDate:&startDate interval:NULL forDate:self]; NSAssert1(ok, @"Failed to calculate the first day of the month based on %@", self); return startDate; } -(int)YHBaseWeekly{ return (int)[[NSCalendar currentCalendar] ordinalityOfUnit:NSDayCalendarUnit inUnit:NSWeekCalendarUnit forDate:self]; } -(int)getYear{ NSCalendar *calendar = [NSCalendar currentCalendar]; NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:self]; return (int)dateComponent.year; } -(int)getMonth{ NSCalendar *calendar = [NSCalendar currentCalendar]; NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:self]; return (int)dateComponent.month; } -(int)getDay{ NSCalendar *calendar = [NSCalendar currentCalendar]; NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:self]; return (int)dateComponent.day; } -(int)getHour{ NSCalendar *calendar = [NSCalendar currentCalendar]; NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:self]; return (int)dateComponent.hour; } -(int)getMinute{ NSCalendar *calendar = [NSCalendar currentCalendar]; NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:self]; return (int)dateComponent.minute; } -(int)getSecond{ NSCalendar *calendar = [NSCalendar currentCalendar]; NSUInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit; NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:self]; return (int)dateComponent.second; } @end </pre><br> <br> <br> <p></p> </pre>
以上这篇iOS自定义UIDatepicker日期选择器视图分享就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。