0

I have updated iOS version to 8.2 but now facing some issues with UIDatePicker and UITextfield keyboard hiding.In iOS 8 UIActionsheet is not supported so I have changed my code to show date Picker but now keyboard is not getting hide when picker opened. here is my code for datepicker :

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];

    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,self.view.bounds.size.width, self.view.bounds.size.height)];
    [myView setBackgroundColor:[UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5]];
    [self.view addSubview:myView];

    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 244, self.view.bounds.size.width, 44)];

    UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissActionSheet)];
    toolbar.items = @[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], done];
    toolbar.barStyle = UIBarStyleBlackOpaque;
    [self.view addSubview:toolbar];

   UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 200, 0, 0)];
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.hidden = NO;
    datePicker.date = [NSDate date];
    datePicker.maximumDate = [NSDate date];
    [datePicker addTarget:self
                   action:@selector(LabelChange:)
         forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:datePicker];
}

but keyboard is not getting hide.

2 Answers2

2

You should call the datePicker creation after some delay. By using code

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //Your code here
    });

this will solve your problem.

NOTE:- But If you are using the date picker as input type for your text field then you should simply create your datePicker in viewDid load and set it as the inputView for your text field -> [textField setInputView:yourPicker]; and it will handle every thing. It will create your date picker on textField tap not the keyBoard.

iHulk
  • 4,869
  • 2
  • 30
  • 39
0

you are showing date picker in "textFieldDidBeginEditing" method then when you are displaying the keyboard ?

vivekDas
  • 1,248
  • 8
  • 12