- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.‘
self.view.backgroundColor = [UIColor orangeColor];
self.datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, 216)];
self.datePicker.datePickerMode = UIDatePickerModeDate;
self.datePicker.backgroundColor = [UIColor groupTableViewBackgroundColor];
[self.view addSubview:self.datePicker];
[self.datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged ];//重点:UIControlEventValueChanged
//设置显示格式
//默认根据手机本地设置显示为中文还是其他语言
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置为中文显示
self.datePicker.locale = locale;
//当前时间创建NSDate
NSDate *localDate = [NSDate date];
//在当前时间加上的时间:格里高利历
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *offsetComponents = [[NSDateComponents alloc] init];
//设置时间
[offsetComponents setYear:0];
[offsetComponents setMonth:0];
[offsetComponents setDay:5];
[offsetComponents setHour:20];
[offsetComponents setMinute:0];
[offsetComponents setSecond:0];
//设置最大值时间
NSDate *maxDate = [gregorian dateByAddingComponents:offsetComponents toDate:localDate options:0];
//设置属性
self.datePicker.minimumDate = localDate;
self.datePicker.maximumDate = maxDate;
_showLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.datePicker.frame)+20, CGRectGetWidth(self.datePicker.frame), 30)];
_showLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
_showLabel.textAlignment = NSTextAlignmentCenter;
_showLabel.text = @"显示获取的日期时间";
[self.view addSubview:_showLabel];
}
nopilot
- 发布:2020-06-30 13:05
- 更新:2020-06-30 13:05
- 阅读:491
0 个回复