iOS UIDatePicker 不显示时间

6
我希望有一个只有一个轮子的日期选择器,填充日期就像日期和时间模式一样。

enter image description here

如果我选择日期模式,我会有3个轮子,但是我会失去日期的名称。实际上,我希望有像日期和时间模式一样的东西,但只有日期。
我可以使用UIPickerView,但我必须自己填充一个非常大的数据数组,因为我想能够回溯很久以前的时间。使用日期选择器,它会自动填充,这很酷。
有解决方案吗?

你只想显示绿色框中的内容吗? - Omkar Guhilot
是的,Omkar,就是这样。 - Eric
5个回答

6
您还可以使用 UIDatePickerpickerMode 属性来实现这一点。
datePicker.pickerMode = .Date

查看头文件,你会发现还有其他选项可供选择。其他值包括:

 Time
 Date
 DateAndTime
 CountDownTimer

2
我检查了所有这些,它们都不符合我的需求。 - Eric

1

你应该使用UIPickerView而不是UIDatePicker。UIDatePicker没有你想要的自定义选项。


0
尝试使用.date显示组件displayedComponents。
DatePicker( selection: .constant(futureDate), displayedComponents: [.date],
                        label: { Text("Due date") })

-1

由于您只需要显示在绿色框中标记的内容,因此您可以使用https://github.com/attias/AADatePicker并进行一些修改,以便根据您的要求获得它。

您需要进行的更改位于AADatePickerView类中

1)在viewForRow方法中注释掉以下内容 enter image description here

2)第二个更改 enter image description here enter image description here


注释掉我提到的代码是你解决方案的快速修复,你可以完全从选择器视图中删除第二个和第三个组件,使其正确。 - Omkar Guhilot
@Eric 这是你需要的吗? - Omkar Guhilot
非常好,谢谢您提供的解决方案,我会尽快实施 :) - Eric

-2
datePicker.datePickerMode = UIDatePicker.Mode.date
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd MMMM yyyy"
        let selectedDate = dateFormatter.string(from: datePicker.date)
        print(selectedDate)
        

2
请不要仅仅发布代码作为答案,还要提供解释您的代码是如何解决问题的。带有解释的答案通常更有帮助和更高质量,并且更有可能吸引赞同。 - Mark Rotteveel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接