ios - UIDatePicker:为时间添加约束

标签 ios uidatepicker datetime-format

有没有办法在 UIDatePicker 上限制/约束时间。

即如果我希望选择器仅显示下午 3:00 至晚上 8:00 的时间,在该时间之前和之后都不会显示,这可能吗?

代码示例会很好。谢谢!

编辑

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{


    // **EDIT 2**
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setMinute:10];
    [comps setHour:5];
    NSDate *minimumTime = [[NSCalendar currentCalendar] dateFromComponents:comps];


    UIDatePicker *pickerView = [[[UIDatePicker alloc] init] initWithFrame:CGRectMake(0, 40, 320, 216)];

    pickerView.datePickerMode = UIDatePickerModeTime;
    [pickerView setMinimumDate:minimumTime];

    [pickerView setMinuteInterval:15];
    [pickerView setTag: kDatePickerTag1];
    //Add picker to action sheet
    [actionSheet addSubview:pickerView];
    NSArray *subviews = [actionSheet subviews];
}

startTimeLabel 是一个 NSString 变量(即 6:30 PM),应该用作 UIPicker 上的最短时间。

图片: enter image description here

最佳答案

使用这段代码:

[datePicker setMaximumDate:yourmaxDate];
[datePicker setMinimumDate:yourminDate];

创建一个 NSDate:

NSDate *todaysDate = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [gregorian components: NSUIntegerMax fromDate:todaysDate];
[comps setMinute:10];
[comps setHour:5];
[comps setSecond:0];
NSDate *yourminDate = [gregorian dateFromComponents:comps];

关于ios - UIDatePicker:为时间添加约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17734941/

相关文章:

ios - 使用 Spritekit 的简单 Gif 动画

java - 无法在 Java 8 中使用 DateTimeFormatter 和 ZonedDateTime 从 TemporalAccessor 获取 ZonedDateTime

java - 将日期和时间字符串转换为特定格式的日期

java - 如何从当前 Locale 获取日期时间格式字符串,作为 SimpleDateFormat 的参数

ios - NSOperation 与 Grand Central Dispatch

ios - swift 中 Base 64 的 RFC2045-MIME 变体

iphone - UIActionSheet - 类未实现 'UIActionSheetDelegate' 协议(protocol)

ios - Swift:一个 UITimePicker,多个文本字段

ios - UIDatePicker 更改 "today"日期文本的颜色

ios - 在 IntelliJ IDEA 中运行 iOS React Native 项目时如何指定设备