Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pop up datePicker when text filed click and disappear when editing done

i have view something like below

enter image description here date picker view is always there how can i make it appear pop up when Enter date is clicked and when i click on back ground then date picker should go down

i have just made date picker view customary but i dont know how to do this appear and disappear thing

like image 936
user2189388 Avatar asked Mar 23 '13 13:03

user2189388


3 Answers

Ok. Here is some sample code for your requirement with animation.

- (void) showView
{
    [self.view addSubview:yourDatePickerView];
        yourDatePickerView.frame = CGRectMake(0, -250, 320, 50);
        [UIView animateWithDuration:1.0
                         animations:^{
                             yourDatePickerView.frame = CGRectMake(0, 152, 320, 260);
                         }];
}

And here is how to hide your DatePickerView

- (void) hideView
{
    [UIView animateWithDuration:0.5
                         animations:^{
                             yourDatePickerView.frame = CGRectMake(0, -250, 320, 50);
                         } completion:^(BOOL finished) {
                             [yourDatePickerView removeFromSuperview];
                         }];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if(textField == yourDateTextField)
    {
         [self showView];
         return NO; // preventing keyboard from showing
    }
    return YES;
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if(textField == yourDateTextField)
    {
         [self hideView];
    }
}

That's all you need.

like image 138
Fire Fist Avatar answered Oct 10 '22 22:10

Fire Fist


You should identify the textfield in the delegate method.

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if(textField == DATE_TEXT_FIELD)
    {
             //Display date picker
    }
}


- (void)textFieldDidEndEditing:(UITextField *)textField
{

        if(textField == DATE_TEXT_FIELD)
        {
                 //Hide date picker
            }
}
like image 21
Apurv Avatar answered Oct 10 '22 22:10

Apurv


  • Create one UIView reference which contains this date picker view.
  • Now assign this datePickerContainerView to textField.inputView property.
  • Then assign textfield.delegate to self and implement your textFieldShouldReturn method. In that method, write this lines

    [textField resignFirstResponder];

    return YES;

Now when you tap on that textfield, it will load datepicker view in place of default keyboard, as per inputview property settings.

like image 1
Mrunal Avatar answered Oct 10 '22 23:10

Mrunal