时间选择器 UIDatePicker

改变时间选择器颜色及选中字体颜色

    self.datePicker = [[UIDatePicker alloc] init];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    self.datePicker.timeZone = [NSTimeZone timeZoneWithName:@"GTM+8"]; // 设置时区,中国在东八区
    self.datePicker.dk_backgroundColorPicker = DKColorPickerWithKey(BAR);
    [self.datePicker setValue:[UIColor colorWithRed:0.651 green:0.714 blue:0.816 alpha:1.000] forKey:@"textColor"];
    [self.datePicker addTarget:self action:@selector(oneDatePickerValueChanged:) forControlEvents:UIControlEventValueChanged]; // 添加监听器
    
    // 设置最小时间
    NSString *minStr = @"1970-01-01";
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *minDate = [dateFormatter dateFromString:minStr];
    self.datePicker.minimumDate = minDate;

    
    // runtime 改变选中时间的颜色
    SEL selector = NSSelectorFromString(@"setHighlightsToday:");
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDatePicker instanceMethodSignatureForSelector:selector]];
    BOOL no = NO;
    [invocation setSelector:selector];
    [invocation setArgument:&no atIndex:2];
    [invocation invokeWithTarget:self.datePicker];

选中方法

#pragma mark - 实现oneDatePicker的监听方法
- (void)oneDatePickerValueChanged:(UIDatePicker *) sender {
    
    NSDate *select = [sender date]; // 获取被选中的时间
    NSDateFormatter *selectDateFormatter = [[NSDateFormatter alloc] init];
    selectDateFormatter.dateFormat = @"yy:MM:dd"; // 设置时间和日期的格式
    NSString *dateAndTime = [selectDateFormatter stringFromDate:select]; // 把date类型转为设置好格式的string类型
    
    // 在控制台打印消息
    NSLog(@"%@----%@", [sender date], dateAndTime);
}

你可能感兴趣的:(时间选择器 UIDatePicker)