Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIDatePicker with UIToolbar

I'm trying to implement UIToolbar on UIDatepicker to dismiss it when touching "Done" button. But when I tap on the button, the datepicker is scrolling and the action button doesn't work.

Here is my code :

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];

My datepicker :

UIDatePicker

Thank you for your help.

like image 843
Thlbaut Avatar asked Jun 19 '15 07:06

Thlbaut


2 Answers

If you use UITextField set

textField.inputAccessoryView = toolbar;
textField.inputView = datepicker;
like image 131
oldrinmendez Avatar answered Nov 04 '22 10:11

oldrinmendez


You have to set inputAccessoryView and InputView to to your UITextField

txtField.inputAccessoryView = toolBar;
txtField.inputView = datePickerView;
like image 34
iAnurag Avatar answered Nov 04 '22 11:11

iAnurag