10

我在表格视图中有一个漂亮干净的 UI,其中有一些文本字段供用户填写。其中一个字段是用户的生日。

我想要它,这样当用户选择生日字段时,会出现一个包含 UIDatePicker 的视图,就像选择文本字段时不同的键盘一样。

这可以做到吗?我将不得不阻止文本字段成为第一响应者(以避免键盘出现),并且如果之前没有显示键盘,我将不得不动画视图向上滑动。

以模态方式呈现视图是否是一种选择?如果是这样,我将如何去做?从文档看来,模态视图仍然占据整个屏幕,我只想使用较低的 216 像素(键盘和 UIDatePicker 的高度)。

有人对如何进行此操作有任何提示吗?

4

4 回答 4

33

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.

于 2013-01-08T23:43:41.780 回答
7

关于动画,看看 DateCell 示例应用程序 - http://developer.apple.com/library/ios/#samplecode/DateCell/Introduction/Intro.html

在任何情况下,正确的方法是设置 UITextField 的 inputView 以显示选择器而不是键盘。这就是它的本意。更多信息在这里: 如何像键盘一样呈现选择器视图?

干杯,

奥德。

于 2011-07-28T14:47:40.263 回答
5

我将通过从屏幕底部向上动画包含 UIDatePicker、完成和取消按钮的视图来实现这一点。使用 CoreAnimation,这应该很容易。

于 2009-12-08T21:44:43.563 回答
1

如果您不想接受来自键盘的用户输入,为什么要使用文本字段?而是使用 UILabel 子类(在其中覆盖 touchesBegan/Ended:withEvent: 方法集以显示 UIDatePicker)或 UIButton(您的操作是向上滑动 UIDatePicker 的方法)。

于 2009-12-08T21:46:25.473 回答