self.datePicker = [[UIDatePicker alloc] init];
self.datePicker.frame = CGRectMake(0, 200, self.view.frame.size.width, 300);
_datePicker.center = self.view.center;
self.datePicker.backgroundColor = [UIColor colorWithRed:127 / 255.0 green:127 / 255.0 blue:127 / 255.0 alpha:1];
NSTimeInterval secondPerDay = 24 * 60 * 60;
NSTimeInterval secondPer2Year = secondPerDay * 356 * 2;
NSDate *today = [[NSDate alloc] init];
// NSDate *max = [today dateByAddingTimeInterval: -secondPer2Year];
NSDate *min = [today dateByAddingTimeInterval: 0];
// 设置为中文
[_datePicker setLocale: [[NSLocale alloc] initWithLocaleIdentifier: @"zh_CN"]];
// 设置日期选择器模式
[_datePicker setDatePickerMode: UIDatePickerModeDate];
// 设置默认选中日期
[_datePicker setDate: today animated: YES];
// 设置允许选择的最小日期
[_datePicker setMinimumDate: min];
// 设置允许选择的最大日期
// [_datePicker setMaximumDate: max];
unsigned int outCount;
int i;
objc_property_t *pProperty = class_copyPropertyList([UIDatePicker class], &outCount);
for (i = outCount -1; i >= 0; i--)
{
// 循环获取属性的名字 property_getName函数返回一个属性的名称
NSString *getPropertyName = [NSString stringWithCString:property_getName(pProperty[i]) encoding:NSUTF8StringEncoding];
NSLog(@"%@",getPropertyName);
if([getPropertyName isEqualToString:@"textColor"])
{
[_datePicker setValue:[UIColor whiteColor] forKey:@"textColor"];
}
}
///修改最大最小时间颜色的BUG
SEL selector = NSSelectorFromString(@"setHighlightsToday:");
//NSInvocation;用来包装方法和对应的对象,它可以存储方法的名称,对应的对象,对应的参数,
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDatePicker instanceMethodSignatureForSelector:selector]];
BOOL no = NO;
[invocation setSelector:selector];
//注意:设置参数的索引时不能从0开始,因为0已经被self占用,1已经被_cmd占用
[invocation setArgument:&no atIndex:2];
[invocation invokeWithTarget:_datePicker];
// 注册当datepicker值改变时触发事件
[_datePicker addTarget: self action: @selector(datePickerChanged:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview: _datePicker];