2

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

  • http://stackoverflow.com/questions/349858/fitting-a-uidatepicker-into-a-uiactionsheet – remyr3my Jan 24 '17 at 10:39
  • Answer given in above link was not working for me. Its showing action sheet but not the picker @Cyph3r –  Jan 24 '17 at 10:52

2 Answers2

0

As UIActionSheet is deprecated in iOS 8.3 you need to go with Apple provided documents and guideline to add DatePicker in your screen.

Apple had introduced the use of inline date picker (like Calendar app date picker).

Use below github Code for Inline Date Picker

Link : https://github.com/DylanVann/DatePickerCell

For reference : iOS 7 - How to display a date picker in place in a table view?

Community
  • 1
  • 1
CodeChanger
  • 7,953
  • 5
  • 49
  • 80
0

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];