带有 UIToolbar 的 UIDatePicker

Posted

技术标签:

【中文标题】带有 UIToolbar 的 UIDatePicker【英文标题】:UIDatePicker with UIToolbar 【发布时间】:2015-09-05 01:57:15 【问题描述】:

我正在尝试在 UIDatepicker 上实现 UIToolbar 以在触摸“完成”按钮时将其关闭。但是当我点击按钮时,日期选择器正在滚动并且操作按钮不起作用。

这是我的代码:

datepicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
[datepicker setDatePickerMode:UIDatePickerModeDate];
[datepicker addTarget:self action:@selector(changeDate:) forControlEvents:UIControlEventValueChanged];

UIToolbar *toolbar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,44)];
toolbar.barStyle = UIBarStyleDefault;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismiss)];

[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];
[datepicker addSubview:toolbar];

我的日期选择器:

感谢您的帮助。

【问题讨论】:

你实现dismiss方法了吗? 【参考方案1】:

如果你使用 UITextField 设置

textField.inputAccessoryView = toolbar;
textField.inputView = datepicker;

【讨论】:

【参考方案2】:

您必须将 inputAccessoryViewInputView 设置为您的 UITextField

txtField.inputAccessoryView = toolBar;
txtField.inputView = datePickerView;

【讨论】:

【参考方案3】:
remove this line from your code:
[datepicker addSubview:toolbar];
and add dismiss method nd add 
txtCurrent.inputAccessoryView = toolbar; in didbeginEditing.

-(void)textFieldDidBeginEditing:(UITextField *)textField 

    txtCurrent = textField;
    [datePicker setHidden:NO];
    txtCurrent.inputAccessoryView = toolbar;
    if ([textField.text isEqualToString:@""]) 

        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        NSDate *eventDate = [NSDate date];
        [dateFormat setDateFormat:@"MM/dd/yyyy"];
        NSString *dateString = [dateFormat stringFromDate:eventDate];
        textField.text = [NSString stringWithFormat:@"%@",dateString];
        
        [textField setInputView:datePicker];

-(void)dismiss
    datePicker.hidden = YES;
    [self.view endEditing:YES];


【讨论】:

【参考方案4】:

试试这个[self.view addSubview:toolbar];

picker = [[UIDatePicker alloc] init];
picker.autoresizingMask = UIViewAutoresizingFlexibleWidth;
picker.datePickerMode = UIDatePickerModeTime;

[picker addTarget:self action:@selector(dueDateChanged:) forControlEvents:UIControlEventValueChanged];
//CGSize pickerSize = [picker sizeThatFits:CGSizeZero];
picker.frame =  CGRectMake(0.0, self.view.frame.size.height - 250, self.view.frame.size.width, 250);
picker.backgroundColor = [UIColor whiteColor];

toolbar= [[UIToolbar alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height - 294,self.view.frame.size.width,44)];
toolbar.barStyle = UIBarStyleDefault;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UIButton* infoButton = [UIButton buttonWithType: UIButtonTypeInfoLight];
[infoButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchDown];

UIBarButtonItem* doneButton =[[UIBarButtonItem alloc]initWithCustomView:infoButton];



[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]];


[self.view addSubview:toolbar];


[self.view addSubview:picker];

【讨论】:

以上是关于带有 UIToolbar 的 UIDatePicker的主要内容,如果未能解决你的问题,请参考以下文章

带有 UIToolbar 的 UIDatePicker

带有 UIToolBar 的 UITableView

将带有两个 UIBarButtonItem 的 UIToolbar 添加到 UINavigationBar:糟糕的 UIToolbar 以及 iPhone 4 怎么样

使用键盘使带有 TextField 的 UIToolbar 向上移动

是否可以隐藏带有动画的 UIToolbar?

Swift - UIPickerView 的带有完成按钮的 UIToolbar 在横向上消失