Skip to content

Instantly share code, notes, and snippets.

@lamprosg
Created June 10, 2015 08:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lamprosg/2370f3f531eaaecc3bb7 to your computer and use it in GitHub Desktop.
Save lamprosg/2370f3f531eaaecc3bb7 to your computer and use it in GitHub Desktop.
(iOS) - DatePicker as keyboard
- (void) initializeTextFieldInputView {
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.minuteInterval = 5;
datePicker.backgroundColor = [UIColor whiteColor];
[datePicker addTarget:self action:@selector(dateUpdated:) forControlEvents:UIControlEventValueChanged];
self.textField.inputView = datePicker;
//Add done button
UIToolbar *toolbar = [[UIToolbar alloc] init];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneButtonWasPressed:)];
UIBarButtonItem *flexibleSeparator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolbar.items = @[flexibleSeparator, doneButton];
self.textField.inputAccessoryView = toolbar;
}
- (void) dateUpdated:(UIDatePicker *)datePicker {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
self.textField.text = [formatter stringFromDate:datePicker.date];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment