您的位置:首页 > 产品设计 > UI/UE

UIDatePicker

2016-07-26 16:33 381 查看
4000

我们需要首先需要设置为中国形式日期,通过属性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;


文/若水V_V(简书作者)

原文链接:http://www.jianshu.com/p/c4f22a6d5f23

著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: