简体   繁体   English

如何在UIDatePicker中禁用AM / PM

[英]How to disable AM/PM in UIDatePicker

How to disable or hide AM/PM in UIDatePicker from code / interface builder? 如何从代码/接口构建器中禁用或隐藏UIDatePicker AM / PM?

I want to have 24 hours time picker mode in UIDatePicker . 我想在UIDatePicker有24小时时间选择器模式。 Please help 请帮忙

According to the documentation you should be able to set the locale property of UIDatePicker to a locale that uses a 24 hrs format. 根据文档,您应该能够将UIDatePicker的locale属性设置为使用24小时格式的语言环境。

NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"da_DK"];
[datePicker setLocale:locale];
[locale release];

Alas there is a bug causing both simulator and device to continue to layout the date picker according to the users international preferences. 唉有一个错误导致模拟器和设备根据用户的国际偏好继续布局日期选择器。

As you suggest, you have to roll your won date picker to be able to display a 24 hrs picker. 正如您所建议的那样,您必须滚动您的赢得日期选择器才能显示24小时选择器。

AFAIK you can't (within limits of SDK). AFAIK你不能(在SDK的范围内)。 The AP/PM will disappear if the user chooses to use 24-hour format. 如果用户选择使用24小时格式,AP / PM将消失。 Make your own UIPickerView in case this is important. 如果这很重要,请创建自己的UIPickerView

the AM/PM is linked with the iphone's locale settings. AM / PM与iphone的语言环境设置相关联。 You can either force the locale of the picker to something that doesn't have AM/PM like ro_RO or in case you want it all the time with AM/PM you can go with en_US. 您可以将选择器的区域设置强制为没有AM / PM的区域,例如ro_RO,或者如果您希望它始终与AM / PM一起使用,则可以使用en_US。 For settings this you can do it in designer on the right side under locale like English(United States) or by code 对于设置,您可以在设备右侧的设置(如英语(美国))或代码下进行设置

[timePicker setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US"]];

I know this is old... but I was looking into this recently and found this through some testing: 我知道这已经老了......但我最近正在研究这个并通过一些测试找到了这个:

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

This basically tells me that the "language" is English (the "en" part) and "time style" is French (which is default 24hr time) (the "FR" part). 这基本上告诉我“语言”是英语(“en”部分),“时间风格”是法语(默认24小时)(“FR”部分)。 I combined the two through trial and error and found that it was working for what I wanted, which was just to have a "Date and Time" picker that also picks in 24 hour time (no AM/PM section on the picker, in English text). 我通过反复试验将两者结合起来,发现它适用于我想要的东西,这只是为了有一个“日期和时间”选择器,也可以在24小时内选择(选择器没有上午/下午部分,英文)文本)。

The problem with this is, when you grab the date here, the locale may be wrong -> 这个问题是,当你在这里获取日期时,语言环境可能是错误的 - >

- (void)changeDate:(UIDatePicker *)sender {

So do this to fix the locale in the date change selection part: 这样做是为了修复日期更改选择部分中的区域设置:

[sender setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_FR"]];

This removes the AM/PM from the picker... 这将从拾取器中移除AM / PM ...

UIDatePicker *myDatePicker = [[UIDatePicker alloc]init];
[myDatePicker setDatePickerMode:UIDatePickerModeDate];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM