Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show a UIDatePicker instead of a keyboard when a user selects a UITextField?

I have a nice clean UI within a table view which has a few text fields for the user to fill out. One of the fields is for the user's birthday.

I'd like to have it so that when the user selects the birthday field, a view containing a UIDatePicker would come up as, just like the different keyboards do when selecting a text field.

Can this be done? I would have to prevent the text field from being the first responder (to avoid having the keyboard come up) and I would have to animate the view sliding up if no keyboard was showing before.

Would presenting the view modally be an option? If so how would I go about doing it? From the documentation it seems that modal views still take up the whole screen, I just want to use the lower 216 pixels (height of the keyboard and UIDatePicker).

Any one have any tips on how to go about doing this?

like image 224
Ben S Avatar asked Dec 08 '09 21:12

Ben S


2 Answers

Old question but the correct way to do this these days would be to set the UITextField's inputView to a picker you created somewhere. Something like this:

UIPickerView *myPicker = [[UIPickerView alloc] init];
// set picker frame, options, etc...
// N.B. origin for the picker's frame should be 0,0

[myTextField setInputView:myPicker];

When you go to edit a UITextField, iOS really just displays whatever view is at textField.inputView which by default is the keyboard, you can make it anything you want as long as it's a subclass of UIView.

like image 110
Endophage Avatar answered Oct 06 '22 01:10

Endophage


Regarding animation, take a look at DateCell sample application - http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html

And in any case, the proper way to do this is set UITextField's inputView to show the picker instead of the keyboard. That's what it's meant to do. More on that here: How can I present a picker view just like the keyboard does?

Cheers,

Oded.

like image 25
Oded Ben Dov Avatar answered Oct 06 '22 00:10

Oded Ben Dov