开发者

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

开发者 https://www.devze.com 2022-12-13 18:08 出处:网络
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 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开发者_如何学Go 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?


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.


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.


I would implement this by just animating a view containing the UIDatePicker, a Done, and Cancel button) up from the bottom of the screen. Using CoreAnimation, this should be pretty easy.


Why are you using a text field if you don't want to accept user input from a keyboard? Instead use a UILabel subclass (where you override the touchesBegan/Ended:withEvent: set of methods to show the UIDatePicker) or a UIButton (where your action is a method which slides up the UIDatePicker).

0

精彩评论

暂无评论...
验证码 换一张
取 消