【问题标题】:Swift Datepicker with minimum date具有最短日期的 Swift Datepicker
【发布时间】:2015-11-27 15:20:17
【问题描述】:

大家早上好,

我有一个带有 ios 8 和 swift 的应用程序。 在UIDatepicker 中有一个UIViewcontroller

我设置了一个最短日期。例如今天的日期:2 |五月 | 2015 使用此解决方案,应该无法设置过去的日期

但如果想将此日期设置为 15 |一月 | 2016 年 我一开始将日期设置为 15 比一个月到 1 月,但随后 UIDatepicker 回到最短日期 2015 年 5 月 2 日

是否有可能,将日期更改为 15 并将月份更改为一月,年份会自动更改为 2016 年?

【问题讨论】:

  • set picker.minimumdate=[nsdate date];
  • 这不是我的问题的解决方案...设置了最短日期。问题是,如果我想设置一个未来的日期,我必须首先选择年份,然后我可以更改日期和月份。
  • 我不相信标准的UIDatePicker 控件可以实现您正在寻找的内容。您必须从头开始实施。
  • @Ghost108,你能实现下面的解决方案吗?

标签: ios date datepicker


【解决方案1】:

让你的 minimumDate 取消设置并尝试通过代码配置它...

试试这个:

@IBAction func changeValue(sender: UIDatePicker) 
{

    //Get time Now, and convert to a NSCalendar
    //Specify the minimun date if you want.
    let now = NSDate()
    let nowCalendar = NSCalendar.currentCalendar()
    let nowComponents = nowCalendar.components([.Day, .Month, .Year], fromDate: now)

    //Compare if date is lesser than now and then create a new date
    if nowCalendar.compareDate(sender.date, toDate: now, toUnitGranularity: [.Day, .Month, .Year]) == NSComparisonResult.OrderedAscending
    {

        let dateCalendar = NSCalendar.currentCalendar()
        let dateComponents = dateCalendar.components([.Day, .Month, .Year], fromDate: sender.date)

        dateComponents.year = nowComponents.year + 1
        let newDate = dateCalendar.dateFromComponents(dateComponents)
        sender.date = newDate!
    }

}

【讨论】:

【解决方案2】:
///Swift4 Version - I think it may works with 3 too.
@IBAction func changeValue(sender: UIDatePicker)
{

    //Get time Now, and convert to a NSCalendar
    //Specify the minimun date if you want.
    let now = Date()
    let nowCalendar = Calendar.current
    let nowComponents = nowCalendar.dateComponents([.day, .month, .year], from: now)

    //Compare if date is lesser than now and then create a new date
    if nowCalendar.compare(sender.date, to: now, toGranularity: Calendar.Component.day) == ComparisonResult.orderedAscending
    {
        var dateCalendar = Calendar.current
        var dateComponents = dateCalendar.dateComponents([.day, .month, .year], from: sender.date)

        guard let year = nowComponents.year else { return }

        dateComponents.year = year + 1
        let newDate = dateCalendar.date(from:dateComponents)
        sender.date = newDate!
    }
}

如果您想玩一点,我发布了一个在操场上工作的完整示例。 https://gist.github.com/dedeexe/4878f78d7e1d5fe8b372ef84de629b59

【讨论】:

  • 我希望这个例子可以帮助你@Shyam
【解决方案3】:

对于 Swift 4:

我有这样的。 1.我的功能:

func AddDaysToToday(days: Int) -> Date? {
    var dateComponents = DateComponents()
    dateComponents.day = days

    return Func.GetCalendar(tz: .utc).date(byAdding: dateComponents, to: Date()) //you can return your own Date here.
}
  1. 在我的 VC 中:

    let today = DateFunc.AddDaysToToday(days: 0)
    datePicker.minimumDate = today
    

【讨论】:

    猜你喜欢
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-29
    • 2021-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多