bendodson / MonthYearWheelPicker

A UIPickerView subclass that allows you to quickly add a picker for just month and year; in most cases it can be used as a drop-in replacement for UIDatePicker.
https://bendodson.com
The Unlicense
184 stars 42 forks source link

Set minimum and maximum date #8

Closed shilpygarg-kiwi closed 3 years ago

shilpygarg-kiwi commented 7 years ago

Hi All,

I am just wondering how will I set the maximum and minimum date of custom monthyearpicker.

Thank for help in advance

yousifa commented 6 years ago

it would be nice if this was implemented

AmirDaliri commented 5 years ago

@shilpygarg-kiwi @yousifa

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        let month = self.selectedRow(inComponent: 0)+1
        let year = years[self.selectedRow(inComponent: 1)]
        if let block = onDateSelected {
            block(month, year)
        }

        self.month = month
        self.year = year
        let currentMonth = Calendar.current.component(.month, from: Date()) - 1
        if year == Calendar.current.component(.year, from: Date()) {
            if month > Calendar.current.component(.month, from: Date()) {
                pickerView.selectRow(currentMonth, inComponent: 0, animated: true)
            }
        }

    }
Pairroxz commented 4 years ago

@shilpygarg-kiwi This is not working, Do any one have find the solution for set minimum and maximum date here.

Rj707 commented 4 years ago
var shouldLimitFutureDate = false
    var shouldLimitPastDate = false

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        let month = self.selectedRow(inComponent: 0)+1
        let year = yearsArray[self.selectedRow(inComponent: 1)]

        let currentMonth = Calendar.current.component(.month, from: Date()) - 1

        if year == Calendar.current.component(.year, from: Date())
        {
            // Selected year is Current Year

            if shouldLimitPastDate
            {
                if month > Calendar.current.component(.month, from: Date())
                {
                    // Selected month is greater than current month
                }
                else if month < Calendar.current.component(.month, from: Date())
                {
                    // Selected month is less than current month

                    pickerView.selectRow(currentMonth, inComponent: 0, animated: true)

                    return
                }
                else
                {
                    // Selected month is equal to current month
                }
            }
            else if shouldLimitFutureDate
            {
                if month > Calendar.current.component(.month, from: Date())
                {
                    // Selected month is greater than current month

                    pickerView.selectRow(currentMonth, inComponent: 0, animated: true)

                    // After Reloading the Components, set the month and year to have the correct data to show

                    dateSelectedWith(month: currentMonth + 1, andYear: year)

                    return
                }
                else if month < Calendar.current.component(.month, from: Date())
                {
                    // Selected month is less than current month
                }
                else
                {
                    // Selected month is equal to current month
                }
            }
            else
            {

            }
        }
        else
        {
            // Selected year is less than or greater than current year

            if year < Calendar.current.component(.year, from: Date())
            {
                // Selected year is less than current year

                if shouldLimitPastDate
                {
                    pickerView.selectRow(currentMonth, inComponent: 0, animated: true)
                    pickerView.selectRow(yearsArray.count - 1, inComponent: 1, animated: false)

                    // After Reloading the Components, set the month and year to have the correct data to show

                    dateSelectedWith(month: currentMonth + 1, andYear: yearsArray[yearsArray.count - 1])

                    return
                }
                else if shouldLimitFutureDate
                {

                }
                else
                {

                }
            }
            if year > Calendar.current.component(.year, from: Date())
            {
                // Selected year greater than current year

                // THAT IS NOT GOING TO HAPPEN EVER!!

                if shouldLimitFutureDate
                {
                    pickerView.selectRow(currentMonth, inComponent: 0, animated: true)

                    return
                }
                else if shouldLimitPastDate
                {

                }
                else
                {

                }
            }
        }

        dateSelectedWith(month: month, andYear: year)

    }

func dateSelectedWith(month:Int, andYear year:Int)
    {
        self.month = month
        self.year = year
        if let block = onDateSelected
        {
            block(month, year)
        }
    }
bendodson commented 3 years ago

There's a couple of options here. Closing this issue as I don't have any plans to update this library currently but if somebody wants to add a pull request to support minimum / maximum dates then feel free.