Search code examples
iosobjective-cuidatepicker

UIDatePicker inside actionsheet is not working


I'm trying to show UIDatePicker inside action sheet but its not working.

Here's my code. On button click I've written this code

 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet];

UIView *datePickerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
[datePickerView setBackgroundColor:[UIColor clearColor]];

CGRect pickerFrame = CGRectMake(0, 0, self.view.frame.size.width, 200);
UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:pickerFrame];
[datePicker setDatePickerMode:UIDatePickerModeDate];

[datePickerView addSubview:datePicker];
[alertController.view addSubview:datePicker];

UIAlertAction* alertAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault                                                           handler:^(UIAlertAction * action) {


}];

[alertController addAction:alertAction];

[self.view.window.rootViewController presentViewController:alertController animated:YES completion:nil];

[alertAction setValue:[UIColor redColor] forKey:@"titleTextColor"];

This is the updated code. While running in simulator it is working fine but working in device I can't able to click the OK button (uialertaction)... While trying to click OK button picker is scrolling continuously


Solution

  • Finally I got the solution. This code works fine in device... Thanks for help

       UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:@"\n\n\n\n\n\n\n\n" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    
    CGRect pickerFrame = datePicker.frame;
    pickerFrame.size.height = datePicker.frame.size.height - 50;
    datePicker.frame = pickerFrame;
    
    [datePicker setDatePickerMode:UIDatePickerModeDate];
    [alertController.view addSubview:datePicker];
    
    UIAlertAction* alertAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault                                                           handler:^(UIAlertAction * action) {
    
    }];
    
    [alertController addAction:alertAction];
    
    [self.view.window.rootViewController presentViewController:alertController animated:YES completion:nil];