如何添加具有工作用户交互的 UIDatePicker 子视图
我有一个基于视图的应用程序,其中有一些文本框,我试图用一些选择器填充这些文本框。例如,其中之一用于日期选择。我使用以下方法将新视图添加到当前视图的底部,然后向上滚动整个视图:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if(textField == [self date]) {
editingDate = YES;
UIDatePicker *pv = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,460,0,0)];
pv.userInteractionEnabled = YES;
[self.view addSubview:pv];
textField.placeholder = @"currently selecting below";
[self scrollTheView:YES];
return NO;
}
return YES;
}
我的scrollTheView方法执行如下:
- (void)scrollTheView:(BOOL) moveUp {
int scrollAmount = 212;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = self.view.frame;
if(moveUp){
rect.origin.y -= scrollAmount;
}
else {
rect.origin.y += scrollAmount;
}
self.view.frame = rect;
[UIView commitAnimations];
}
我的问题是,虽然显示看起来很完美,但日期选择器控件不会接受我的完全输入。它只是停留在当前日期。我不确定我在这里缺少什么,可能很简单。
I have a view based application which has some textboxes that I'm trying to populate with some pickers. For example, one of them will be for date selection. I'm using the following to add a new view to the bottom of my current view, then scroll the entire view upwards:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if(textField == [self date]) {
editingDate = YES;
UIDatePicker *pv = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,460,0,0)];
pv.userInteractionEnabled = YES;
[self.view addSubview:pv];
textField.placeholder = @"currently selecting below";
[self scrollTheView:YES];
return NO;
}
return YES;
}
My scrollTheView method executes as follows:
- (void)scrollTheView:(BOOL) moveUp {
int scrollAmount = 212;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.3];
CGRect rect = self.view.frame;
if(moveUp){
rect.origin.y -= scrollAmount;
}
else {
rect.origin.y += scrollAmount;
}
self.view.frame = rect;
[UIView commitAnimations];
}
My problem is that although the display looks perfect, the datepicker control will not accept my input at all. It is simply stuck on the current date. I'm not sure what I'm missing here, probably something simple.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过修改上面的代码执行以下操作,将子视图添加为超级视图的子视图:
并修改我的滚动方法以另外滚动这个新视图:
我能够实现目标。感谢本的帮助。
By modifying the code above to do the following to add the subview as a subview of the superview:
and modifying my scroll method to additionally scroll this new view:
I was able to achieve the goal. Thanks to Ben for the assistance.
从您的描述看来,您将 UIDatePicker 放置在另一个视图内,但在其边界之外。如果未选中 IB 中的“剪辑子视图”,那么您仍然可以看到选择器,但由于它不在边界区域中,因此无法触摸。
It sounds from what you've described that you're placing your UIDatePicker inside of another view, but outside of its bounds. If "Clips subviews" in IB is not checked, then you'll still be able to SEE your picker, but since it's not in the bounds area, it will not be touchable.