【问题标题】:Limit the selectable dates in native datepicker in iOS Phonegap App在 iOS Phonegap App 中限制本机日期选择器中的可选日期
【发布时间】:2014-09-19 22:57:35
【问题描述】:

我正在使用 Phonegap BuildiOS 开发应用程序,并且我正在使用输入数据类型来调用本机日期选择器(没有问题)并选择一些日期,但我想限制可选择的日期。

我已尝试使用输入的minmax 属性,但被忽略了。在 iOS 6 和 iOS 7 上测试。

这是我的代码:

<input id="fecha-buscar" type="date" min="2014-01-01" max="2014-12-31" value="2014-01-01">

我找到了this similar SO question,但没有答案。

知道如何解决这个问题吗?可以修复&lt;input type="date"&gt;minmax属性,也可以调用iOS的native datepicker

谢谢!

【问题讨论】:

  • 不幸的是,我也遇到了这个问题。贾贝尔,你找到解决办法了吗?我觉得 Cordova 的 datepicker 插件是一种方式

标签: html ios cordova datepicker


【解决方案1】:

fwiw,使用原生 cordova(不是 pg 构建),我想我会分享这项工作。

在 MainViewController.m 中

- (void)viewDidLoad
{
         :
    /* add this somewhere */
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_pickerViewWillBeShown:) name: UIKeyboardWillShowNotification object:nil];
         :
}

- (void)_pickerViewWillBeShown:(NSNotification*)aNotification {
    [self performSelector:@selector(_resetPickerViewBackgroundAfterDelay) withObject:nil afterDelay:0];
}

-(void)_resetPickerViewBackgroundAfterDelay
{
    UIPickerView *pickerView = nil;
    for (UIWindow *uiWindow in [[UIApplication sharedApplication] windows]) {
        for (UIView *uiView in [uiWindow subviews]) {
            pickerView = [self _findPickerView:uiView];
        }
    }

    if (pickerView){
        NSDate *now = [NSDate date];
        NSCalendar *calendar = [[NSCalendar alloc]    initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:now];
        //set for today at 8 am
        [components setHour:8];
        NSDate *todayAtTime = [calendar dateFromComponents:components];
        //set max at now + 60 days
        NSDate *futureDate = [NSDate dateWithTimeIntervalSinceNow:60 * 60 * 24 * 60];

//        [pickerView setBackgroundColor:[UIColor greenColor]];
        [pickerView.superview setValue:@"15" forKey:@"minuteInterval"];
        [pickerView.superview setValue:futureDate forKey:@"maximumDate"];
        [pickerView.superview setValue:todayAtTime forKey:@"minimumDate"];
    }
}

-(UIPickerView *) _findPickerView:(UIView *)uiView
{
        if ([uiView isKindOfClass:[UIPickerView class]] ){
            return (UIPickerView*) uiView;
        }

        if ([uiView subviews].count > 0) {
            for (UIView *subview in [uiView subviews]){
                UIPickerView* view = [self _findPickerView:subview];
                if (view)
                    return view;
            }
        }
        return nil;
}

使用 iOS 6/7、Cordova 3.5、devices/sim 测试

【讨论】:

  • 我正在使用 Phonegap Build 而不是 Cordova 编译我的应用程序,我该如何使用此代码?
  • 我认为您必须开发并提交一个插件供他们考虑。不确定他们是否允许自定义插件。有点为什么我不使用 PG Build...几年来一直在使用这项工作
【解决方案2】:

我刚刚为 phonegap/cordova 找到了一个 datepicker 插件,它有最小值和最大值的选项。 https://github.com/sectore/phonegap3-ios-datepicker-plugin/tree/ios7

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 2018-05-26
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多