views:

576

answers:

2

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.

A: 

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.

Ben Gottlieb
Yep, that's it. I can see it but not use it. If I do check 'clips subviews', then I can't even see the subviews though... so what is the real solution here? Is my approach just entirely wrong?
sw
It sounds like you're heading down the wrong path. Try adding the UIDatePicker to your viewController's view property. If that's the same thing you're manipulating in scrollTheView, DON'T. You should never adjust the bounds of the view. Try moving stuff into subviews and moving those around.
Ben Gottlieb
Ah, my complexity is that I do want to move my entire initial view upwards so that when the new datepicker scrolls up from the bottom, it is essentially 'pushing' the existing view upwards. I'm currently adding the new picker using [self.view addSubview:picker]; self.view.frame's origin is what i have been moving up so far in the method code above.
sw
+1  A: 

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.

sw