自定义一个只显示年月的DatePicker(UIDatePicker无法实现年月显示) - 华过的痕迹

时间:2024-02-24 22:44:38

自定义一个只显示年月的DatePicker(UIDatePicker无法实现年月显示)

HooDatePicker 介绍(introduction)

==================================================
项目需要一个DatePicker,只显示年月,而且选中的时间高亮显示(苹果默认的只显示灰色),研究多个代码后没有达到预想效果,没办法只能动手丰衣足食,用了一天时间封装出这个性能比较高的自定义DatePicker。苹果原生的UIDatePicker无法实现只提供年月选择,而HooDatePicker增加了年月显示,并提供了扁平化和样式美化,也更加符合中国人的时间习惯。
A customized DatePicker. which can show only Years and months.the UIDatePicker don\'t achive this. and HooDatePicker looks much more beautiful than UIDatePicker.I hope you can like it.

Github address

==================================================
(Github下载地址):https://github.com/jakciehoo/HooDatePicker

效果图展示(picure show):

==================================================
HooDatePickerModeDate Demo picture:


Simulator Screen Shot Mar 6, 2016, 3.04.09 AM.png

HooDatePickerModeYearAndMonth Demo picture


Simulator Screen Shot Mar 6, 2016, 3.04.30 AM.png

如何使用How To Use:

==================================================

HooDatePicker *datePicker = [[HooDatePicker alloc] initWithSuperView:self.view];
datePicker.delegate = self;
datePicker.datePickerMode = HooDatePickerModeYearAndMonth;
[datePicker show];
[datePicker dismiss];

 

 

更多用法More:

==================================================
1.设置其他属性

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
NSDate *maxDate = [dateFormatter dateFromString:@"01-01-2050 00:00:00"];
NSDate *minDate = [dateFormatter dateFromString:@"01-01-2016 00:00:00"];
[self.datePicker setDate:[NSDate date] animated:YES];//设置默认日期
self.datePicker.minimumDate = minDate;//设置显示的最小日期
self.datePicker.maximumDate = maxDate;//设置显示的最大日期
[self.datePicker setTintColor:[UIColor redColor]];//设置主色
[self.datePicker setHighlightColor:[UIColor yellowColor]];//设置高亮颜色

 

2.相关代理

@protocol HooDatePickerDelegate<NSObject>
@optional
- (void)datePicker:(HooDatePicker *)datePicker dateDidChange:(NSDate *)date;
- (void)datePicker:(HooDatePicker *)datePicker clickedCancelButton:(UIButton *)sender;
- (void)datePicker:(HooDatePicker *)datePicker clickedSureButton:(UIButton *)sender date:(NSDate*)date;
@end

 

For more detail, you can download this project and see demo code in it.

Enjoy it!!

欢迎关注我的微信公众号“丁丁的coding日记”,一起学习iOS开发技术