【问题标题】:How to disable future dates,months and year in UIDatePicker?如何在 UIDatePicker 中禁用未来的日期、月份和年份?
【发布时间】:2014-07-17 14:53:24
【问题描述】:

如何在 UIDatePicker 中隐藏未来的日期,以便用户仅选择过去和当前的生日年份。

我搜索了很多来源,但我无法得到想要的结果。

这是我的代码,

 dateofBirthDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];

    dateofBirthDatePicker.datePickerMode = UIDatePickerModeDate;
    [dateofBirthDatePicker setBackgroundColor:DATE_PICKER_GRAY_COLOR];

//    UILabel *label = [UILabel appearanceWhenContainedIn:[UITableView class],[UIDatePicker class], nil];
//    label.font = HELVETICA_NEUE(24);
//    label.textColor = GREEN_COLOR;
    [dateofBirthDatePicker addTarget:self
                              action:@selector(LabelChange:)
                    forControlEvents:UIControlEventValueChanged];
    dateofbirthTextField.inputView = dateofBirthDatePicker;
    [self datePickerToolBar];
}

- (void)LabelChange:(id)sender
{
    NSDateFormatter *dateFormat= [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    dateofbirthTextField.text = [NSString stringWithFormat:@"%@",
                                [dateFormat stringFromDate:dateofBirthDatePicker.date]];
}

如果有人知道解决方案,请提出建议。我真的很感激你。

【问题讨论】:

    标签: ios objective-c uidatepicker


    【解决方案1】:

    这是防止未来日期选择的简单代码:

    dateofBirthDatePicker.maximumDate=[NSDate date];
    

    在斯威夫特中:

    dateofBirthDatePicker.maximumDate = Date()
    

    【讨论】:

      【解决方案2】:

      在 Swift 2 中:

      self.datePicker.maximumDate = NSDate()

      在 Swift 3 中:

      self.datePicker.maximumDate = Date()

      【讨论】:

        【解决方案3】:

        在 Swift 4.0.3 Xcode 9 中

        隐藏未来日期

        self.picker.maximumDate = Date()
        

        【讨论】:

          【解决方案4】:

          您可以在 UIDatePicker 对象上使用 setMaximumDate: 和 setMinimumDate::

          [dateofBirthDatePicker setMaximumDate:[NSDate date]]; // The max date will be today
          
          // Optional you can set up min date as well
          [dateofBirthDatePicker setMinimumDate:yourMinDate];
          

          【讨论】:

            【解决方案5】:
            NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
            NSDate *currentDate = [NSDate date];
            NSDateComponents *comps = [[NSDateComponents alloc] init];
            [comps setYear:1];
            NSDate *maxDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
            [comps setYear:-30];
            NSDate *minDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
            
            [datePicker setMaximumDate:maxDate];
            [datePicker setMinimumDate:minDate];
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2011-03-21
              • 2021-12-16
              • 1970-01-01
              • 1970-01-01
              • 2014-09-18
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多