添加日期选择器视图和uitoolbarview到您的视图。 例如
CGFloat toolbarHeight = 44.f;
CGFloat datePickerHeight = 140.f;
CGFloat containerViewHeight = toolbarHeight+datePickerHeight;
//create container view for datepicker and toolbar
UIView *containerView = [[UIVIew alloc] initWithFrame:CGRectMake(0.f, [self.view bounds].size.height+containerViewHeight, [self.view bounds].size.width, containerViewHeight)];
//after init toolbar set the frame
toolBar.frame = CGRectMake(0.f,0.f,containerView.frame.size.width, toolbarHeight];
//then add on toolbar button save and release it after add
//after init datePicker set the frame
datePicker.frame = CGRectMake(0.f, toolbarHeight, containerView.frame.size.width, datePickerHeight);
//add datePicker and toolBar to containerView
[containerView addSubview: toolBar];
[containerView addSubview: datePicker];
[toolBar release];
[datePicker release];
//add containerView to main view
[self.view addSubview: containerView];
[containerView release];
所以当视做负载它们会被隐藏contener鉴于Y为您的视图框架之外。
现在你应该分配财产委托UITextFieldDelegate
textfield.delegate = self;
的在委托textFieldDidBeginEditing的方法看到UITextFieldDelegate你应该火法,将显示您的日期选择器和工具栏。 如果你有很多文本框来定义选择使用标签属性,它是整数,它是您可以使用开关,而不是许多如果条件。
要显示您的datePicker和工具栏,您应该更改containerView框架的Y. 这是怎么做的
CGRect containerViewFrame = containerView.frame;
containerViewFrame.y -=containerViewHeight;
containerView.frame = containerViewFrame;
隐藏只是加上containerViewHeight变量。 您可以使用UIViewAnimations以某种动画显示它。例如UIViewAnimationCurveEaseInOut
的出口这正是我做什么,但我忘了更新帖子。 – lolol
根据这个答案,我写了一个开源类,[STADateField](https://github.com/Stunner/STAControls/blob/master/STAControls/STAControls/TextField/STADateField.h)。 – Stunner