IOS UIdatePicker 改变背景颜色,也可以根据相应的思路添加背景图片
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLocale *ch_zh_locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans_CN"] ;
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 100, 320, 216)];//创建datePicker实例变量 并初始化
datePicker.backgroundColor = [UIColor clearColor];
datePicker.locale = ch_zh_locale; //设置中文显示
[datePicker setDatePickerMode:UIDatePickerModeDateAndTime]; //设置显示模式
[datePicker setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]]; //设置时区,上海时区
[datePicker setMinimumDate:[NSDate date]]; //设置控件的最小时间,超出最小时间还小则会弹回,此时时间为中国时间
[datePicker setMaximumDate:[NSDate dateWithTimeIntervalSinceNow:30*24*60*60]];//设置最大时间,超出也会弹回
[datePicker addTarget:self action:@selector(timeChanged:) forControlEvents:UIControlEventValueChanged];// 关联方法
//0-大背景的UIview;0-0各个小格子的UIview;0-1控件选中框上方那条横线;0-2控件选中框下方那条横线
UIView *pickerView = [[datePicker subviews] objectAtIndex:0];//获取控件的子视图
//将自己定义UIView 加在 控件的子视图上
UIView *colorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
colorView.backgroundColor = [UIColor blueColor];
colorView.alpha = 0.2f;
colorView.center = pickerView.center;
[pickerView addSubview:colorView];
[self.view addSubview:datePicker];
}
- (void)timeChanged:(id)sender
{
UIDatePicker *currentPicker = (UIDatePicker *) sender;
NSDate *pickerDate = [currentPicker date];//格林尼治时间
NSLog(@"datepicker current time %@",[pickerDate dateByAddingTimeInterval:8*60*60]); // + 8 输出的格式是24制
}
UIdatePicker是苹果的系统控件,是不允许定制的,如果定制了发布的时候也有可能被苹果拒绝,建议使用UIPickerView