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

后端 未结 4 1973
忘了有多久
忘了有多久 2021-02-14 14:32

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 t

相关标签:
4条回答
  • 2021-02-14 15:12

    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.

    0 讨论(0)
  • 2021-02-14 15:21

    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 讨论(0)
  • 2021-02-14 15:23

    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.

    0 讨论(0)
  • 2021-02-14 15:23

    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.

    0 讨论(0)
提交回复
热议问题