用UIPickView自定义UIDatePickView

//创建pickView

UIPickerView *pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(20, 40, ScreenWidth-30, self.frame.size.height-50)];

pickView.delegate = self;

pickView.dataSource = self;  self.pickView = pickView;

pickView.showsSelectionIndicator = NO;

[self addSubview:pickView];

NSCalendar *gregorian = [[NSCalendar alloc]

initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

NSDate* dt = [NSDate date];

unsigned unitFlags = NSCalendarUnitYear |

NSCalendarUnitMonth |  NSCalendarUnitDay |

NSCalendarUnitHour |  NSCalendarUnitMinute |

NSCalendarUnitSecond | NSCalendarUnitWeekday;

NSDateComponents* comp = [gregorian components: unitFlags

fromDate:dt];

 

self.year = comp.year;

self.month = comp.month;

self.day = comp.day;

 

NSArray *yearArray = @[@(comp.year  -5),@(comp.year-4),@(comp.year-3),@(comp.year-2),@(comp.year-1),@(comp.year),@(comp.year+1),@(comp.year+2),@(comp.year+3),@(comp.year+4),@(comp.year+5)];

self.yearArray = yearArray;

 

NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

NSDate * currentDate = [NSDate date];

NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay

inUnit: NSCalendarUnitMonth

forDate:currentDate];

self.dayCount = range.length;

 

[pickView selectRow:5 inComponent:0 animated:YES];

[pickView selectRow:self.month-1 inComponent:1 animated:YES];

[pickView selectRow:self.day-1 inComponent:2 animated:YES];

}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

return 3;

}

 

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

if (component == 0) {

return self.yearArray.count;

}else if (component == 1) {

return 12;

}else if (component == 2){

//判断月份有多少天 包括2月份的具体天数

if((self.month == 1) || (self.month == 3) || (self.month == 5) || (self.month == 7) || (self.month == 8) || (self.month == 10) || (self.month == 12))

return 31 ;

 

if((self.month == 4) || (self.month == 6) || (self.month == 9) || (self.month == 11))

return 30;

 

if((self.year % 4 == 1) || (self.year % 4 == 2) || (self.year % 4 == 3))

{

return 28;

}

 

if(self.year % 400 == 0)

return 29;

 

if(self.year % 100 == 0)

return 28;

 

return 29;

 

}else{

return 0;

}

 

}

 

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

if (component == 0) {

return [NSString stringWithFormat:@”%@”,self.yearArray[row]];

}else if (component == 1){

return [NSString stringWithFormat:@”%ld”,row+1];

}else if (component ==2){

return [NSString stringWithFormat:@”%ld”,row+1];

}else{

return nil;

}

}

 

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

if (component == 0) {

self.year = [self.yearArray[row] integerValue];

}else{

if (component == 1) {

self.month = row + 1;

}if (component == 2) {

self.day =row + 1;

}

}

 

[self.pickView reloadComponent:2];

 

NSString *timeMessage = [NSString stringWithFormat:@”%ld-%ld-%ld”,(long)self.year,(long)self.month,(long)self.day];

if (self.myBlock) {

self.myBlock(timeMessage);

}

 

 

}

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

UILabel* pickerLabel = (UILabel*)view;

//自定义字体

if (!pickerLabel){

pickerLabel = [[UILabel alloc] init];

// Setup label properties – frame, font, colors etc

//adjustsFontSizeToFitWidth property to YES

[pickerLabel setBackgroundColor:[UIColor clearColor]];

[pickerLabel setFont:[UIFont boldSystemFontOfSize:13]];

}

// Fill the label text here

pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];

pickerLabel.textAlignment = NSTextAlignmentCenter;

//隐藏分割线

[[pickerView.subviews objectAtIndex:1] setHidden:TRUE];

[[pickerView.subviews objectAtIndex:2] setHidden:TRUE];

 

return pickerLabel;

}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{

return 35;

}

Add a Comment

电子邮件地址不会被公开。 必填项已用*标注