本文為CocoaChina網友piggybear投稿
日期選擇器,支持年、年月、年月日、時分、年月周 時分等。
由於使用UIPickerView的話,列表會有個弧度,所以這裡用了PGPickerView
GitHub地址
https://github.com/xiaozhuxiong121/PGDatePicker
CocoaPods安裝
pod 'PGDatePicker'
使用
PGDatePicker *datePicker = [[PGDatePicker alloc]init]; datePicker.delegate = self; [datePicker show]; datePicker.datePickerMode = PGDatePickerModeYear; #pragma PGDatePickerDelegate - (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents { NSLog(@"dateComponents = %@", dateComponents); }
設置Date
建議用NSDate+PGCategory類所定義的方法去設置
+ (NSDate *)setYear:(NSUInteger)year; + (NSDate *)setYear:(NSUInteger)year month:(NSUInteger)month; + (NSDate *)setYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day; + (NSDate *)setMonth:(NSUInteger)month day:(NSUInteger)day hour:(NSInteger)hour minute:(NSInteger)minute; + (NSDate *)setHour:(NSInteger)hour minute:(NSInteger)minute;
比如:datePicker.maximumDate = [NSDate setYear:2017];