gpt4 book ai didi

ios - UIDatePicker 最小/最大日期

转载 作者:行者123 更新时间:2023-11-29 02:50:57 24 4
gpt4 key购买 nike

我创建了一个 UIDatePicker 以便用户可以选择他们的生日,并且我尝试设置允许的最小和最大日期。但是,像我在下面所做的那样设置最小/最大日期只会使超出范围的日期变灰,但您仍然可以滚动到它们。如何防止日期选择器滚动到这些日期而不是仅仅将它们灰显?

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setDay:1];
[comps setMonth:1];
[comps setYear:1914];
NSDate *minimumDate = [[NSCalendar currentCalendar] dateFromComponents:comps];

NSDateComponents *compsMax = [[NSDateComponents alloc] init];
[compsMax setDay:31];
[compsMax setMonth:12];
[compsMax setYear:2013];
NSDate *maximumDate = [[NSCalendar currentCalendar] dateFromComponents:compsMax];

birthdayPicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(220, 150, 500, 10)];
[birthdayPicker setDatePickerMode:UIDatePickerModeDate];
[birthdayPicker setMinimumDate:minimumDate];
[birthdayPicker setMaximumDate:maximumDate];
[birthdayPicker reloadInputViews];
[self addSubview:birthdayPicker];

最佳答案

我承认,它不是最好的,但试一试,也许它可以改进:

viewDidLoad

[birthdayPicker addTarget:self
action:@selector(pickerValueChanged:)
forControlEvents:UIControlEventValueChanged];

代码中的其他地方

- (IBAction)pickerValueChanged:(id)sender {
dispatch_async(dispatch_get_main_queue(), ^{
UIDatePicker *datePicker = (UIDatePicker *)sender;
if ([datePicker.date compare:datePicker.maximumDate] == NSOrderedDescending) {
datePicker.date = datePicker.maximumDate;
}
else if ([datePicker.date compare:datePicker.minimumDate] == NSOrderedAscending) {
datePicker.date = datePicker.minimumDate;
}

});
}

关于ios - UIDatePicker 最小/最大日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24539575/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com