How can I add a UIDatePicker Subview with working user interaction
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];
textFie开发者_开发问答ld.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.
By modifying the code above to do the following to add the subview as a subview of the superview:
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 480, 320, 216)];
UIDatePicker *pv = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,0,320,216)];
[myView addSubview:pv];
[self.view.superview addSubview:myView];
and modifying my scroll method to additionally scroll this new view:
UIView *sv = self.view.superview;
UIView *subv = [[sv subviews] objectAtIndex:1];
CGRect rect2 = subv.frame;
if(moveUp){
rect2.origin.y -= scrollAmount;
}
else {
rect2.origin.y += scrollAmount;
}
subv.frame = rect2;
I was able to achieve the goal. Thanks to Ben for the assistance.
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.
精彩评论