UIDatePicker日期选择器

在设置日期像生日的时候会使用到UIDatePicker。

我们需要首先需要设置为中国形式日期,通过属性Locale设置DatePicker的地区,即设置DatePicker显示的语言。

1.如果想查看跟踪所有可用的地区标识,取出想要的地区

 NSLog(@"%@", [NSLocale availableLocaleIdentifiers]);

2.设置日期选择控件的地区,中国地区标识是:"zh_Hans_CN"或者"zh_CN"

[datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];

下面是常用的创建一个日期选择器方法:

dateView = [[UIButton alloc] initWithFrame:self.view.bounds];
[self.view addSubview:dateView];
NSLog(@"%@", [NSLocale availableLocaleIdentifiers]);
// 初始化UIDatePicker,旋转滚动选择日期类
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 350, 320, 216)];

[datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"]];
// 设置时区
[datePicker setTimeZone:[NSTimeZone localTimeZone]];

// 设置当前显示时间
[datePicker setDate:[NSDate date] animated:YES];
// 设置显示最大时间(此处为当前时间)
[datePicker setMaximumDate:[NSDate date]];
// 设置UIDatePicker的显示模式
[datePicker setDatePickerMode:UIDatePickerModeDate];
// 当值发生改变的时候调用的方法
[datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
[dateView addSubview:datePicker];
[dateView addTarget:self  action:@selector(hideDateView) forControlEvents:UIControlEventTouchUpInside];
dateView.hidden = YES;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容