0

I want to add programmatically a modal UIAlertController with a UIDatePicker in it to pick a date in iOS 7. The picker works fine but the height of the UIAlertController is not heigh enough to show the date picker. Solving this is probably very simple but I can't find the solution.

-(void) iphoneActionsheet {
    self.searchActionSheet=[UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
    self.searchActionSheet.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    NSInteger iPhoneWidth = self.view.frame.size.width;
    self.searchActionSheet.view.frame = CGRectMake(0, 0, iPhoneWidth, 250);
    [ self.searchActionSheet.view setBounds:CGRectMake(0, 0, iPhoneWidth, 250)];

    UIView *datepickerView = [UIView new];
    datepickerView.frame = CGRectMake(0, 0, self.view.frame.size.width, 250);

    UIDatePicker *datePicker=[UIDatePicker new];//Date picker
    datePicker.frame=CGRectMake(0,0,iPhoneWidth, 200);
    datePicker.datePickerMode = UIDatePickerModeTime;
    [datePicker setMinuteInterval:1];
    [datePicker setTag:10];
    if(self.activeDate)[datePicker setDate:self.activeDate];
    [datePicker addTarget:self action:@selector(datePickerResult:) forControlEvents:UIControlEventValueChanged];
    [datepickerView addSubview:datePicker];


    [self.searchActionSheet.view addSubview:datepickerView];
    [self presentViewController:self.searchActionSheet animated:YES completion:nil];

}

enter image description here

Olivier de Jonge
  • 1,454
  • 2
  • 15
  • 31

1 Answers1

0

You cannot set the height of a UIAlertController due to Apple stating in the Human Interface Guidelines that this control should be used to display short user messages as opposed to other controls or walls of text.

I will suggest to use UIPopoverController and implement your own dismiss button, since UIAlertController's purpose is more to display alert messages to the user (short message) per Apple Documentation.

Find out more information about UIPopoverController here :

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIPopoverController_class/

  • 1
    The documentation states "Popover controllers are for use exclusively on iPad devices. Attempting to create one on other devices results in an exception." and I'm using this here for an iPhone – Olivier de Jonge Sep 03 '15 at 10:20
  • Some posts suggest to use a UIActionSheet, but I have the same problem there and the component seems not to be compatible anymore with ios 8. BTW the style of the UIAlertController is set to: preferredStyle:UIAlertControllerStyleActionSheet. – Olivier de Jonge Sep 03 '15 at 10:28