ios 在IOS中设置Datepicker只选择月份和年份
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6328175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the Datepicker in IOS to pick only the Month and Year
提问by Paul John Parreno
Is there any way possible to set a UIDatePicker in XCode-Interface Builder to just use the rollers of the "month" and "year" without the "day"? I've tried checking the file properties in the IB yet failed to find a solution for my problem...
有没有办法在 XCode-Interface Builder 中设置 UIDatePicker 只使用“月”和“年”的滚轮而不使用“日”?我已经尝试检查 IB 中的文件属性,但未能找到解决我的问题的方法...
回答by Deepak Danduprolu
Your question mentions date
and year
so I it seemed like UIDatePickerModeDate
would suffice but as you are looking for month
and year
which is not an available option. I suggest you consider using a two component UIPickerView
object.
你的问题提到date
,year
所以我似乎UIDatePickerModeDate
就足够了,但正如你正在寻找的那样month
,year
这不是一个可用的选项。我建议您考虑使用两个组件UIPickerView
对象。
Original Answer
原答案
You can do this by changing the Mode
property under Date Picker
to Date
in the Attributes Inspector
in the right bar ( Cmd + Option + 4 ). You can also do this programmatically,
您可以通过改变这样做Mode
下的财产Date Picker
,以Date
在Attributes Inspector
在右边栏(Cmd的+期权+ 4)。您也可以以编程方式执行此操作,
datePicker.datePickerMode = UIDatePickerModeDate;
回答by gnuchu
I don't think this is possible using the standard UIDatePicker. If you have a look at the documentationit makes it clear that only four modes are available:
我认为使用标准 UIDatePicker 是不可能的。如果您查看文档,它会清楚地表明只有四种模式可用:
- UIDatePickerModeTime,
- UIDatePickerModeDate,
- UIDatePickerModeDateAndTime,
- UIDatePickerModeCountDownTimer
- UIDatePickerModeTime,
- UIDatePickerModeDate,
- UIDatePickerModeDateAndTime,
- UIDatePickerModeCountDownTimer
You may have to create your own.
您可能必须创建自己的。
回答by Igor
You can use this Month and Year picker realization
您可以使用此月份和年份选择器实现
回答by anand sankaran
You can use UICustomDatePickerclass to show only the needed date components
您可以使用UICustomDatePicker类仅显示所需的日期组件
回答by orkoden
What you can do is restrict the range of dates, that can be used for input using the properties maximumDate and minimumDate
您可以做的是限制日期范围,可用于使用属性 maximumDate 和 minimumDate进行输入
This has the added benefit of being automatically localized. i.e. the order of month and day is correct.
这具有自动本地化的额外好处。即月和日的顺序是正确的。
回答by Puneet Sharma
The question has been answered but for someone who is looking for a UIComponent that selects only month and year, there is a tool at github that can be used:
该问题已得到解答,但对于正在寻找仅选择月份和年份的 UIComponent 的人来说,github 上有一个可以使用的工具:
回答by Shubham Singh
You can use Third Party custom Date Picker as iOS Default Date Picker do not provide one.
您可以使用第三方自定义日期选择器,因为 iOS 默认日期选择器不提供。
Try https://github.com/nairteashop/NTMonthYearPicker.
试试https://github.com/nairteashop/NTMonthYearPicker。
Worked for me
为我工作