Add UIDatePicker to UIAlertView

I am trying to add a date picker to the alert view. I can add animation to the screen based on the click of a button, and I see a black box, but no date picker.

Here is what I still have ...

- (IBAction)showDatePicker { CGRect frame = CGRectMake(200, self.view.frame.size.height, self.view.frame.size.width, 0); //CGRectMake(225, 145, 260, 125); UIPickerView *datePicker = [[UIPickerView alloc] initWithFrame:frame]; UIAlertView *showDateAlert = [[UIAlertView alloc] initWithTitle:@"Enter the Code Date" message:@"Sample message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Update", nil]; [self.view addSubview:datePicker]; [UIView beginAnimations:@"slideIn" context:nil]; [datePicker setCenter:CGPointMake(datePicker.frame.origin.x, self.view.frame.size.height - datePicker.frame.size.height/2)]; [UIView commitAnimations]; } 

I found an example that seems to work, but I do not see any dialog boxes or buttons in my warning window, and I select the date myself. Did I miss something?

 - (IBAction)showDatePicker { CGRect frame = CGRectMake(225, self.view.frame.size.height, self.view.frame.size.width, 125); UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:frame]; datePicker = [[UIDatePicker alloc] init]; datePicker.datePickerMode = UIDatePickerModeDate; [datePicker setDate:[NSDate date]]; UIAlertView *alert; alert = [[UIAlertView alloc] initWithTitle:@"Enter the Code Date" message:@"Sample message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Update", nil]; alert.delegate = self; [alert addSubview:datePicker]; [alert show]; } 
+6
source share
6 answers

I agree with commentators who said that this is not a way to do this for several reasons. First, no one likes pop-up warnings all the time. Secondly, and not applicable to your situation. Ultimately, this can lead to a rejection of the application. Apple changed the wording in the UIAlertView class description, referring to its view hierarchy as closed; And we all know how the apple feels that you are fucking in what is considered private.

From the link to the UIAlertView class:

The UIAlertView class is intended to be used as is, not subclass support. The presentation hierarchy for this class is private and should not be changed.

But since you are saying that this is a private application, this is the case here. A UIAlertView is just a subclass of UIView . Thus, all frame and border rules still apply. Here's a basic example of how to set the collector frame and alert borders to compress the dates in the collector.

 // Create alert UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; // Show alert (required for sizes to be available) [alert show]; // Create date picker (could / should be an ivar) UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, alert.bounds.size.height, 320, 216)]; // Add picker to alert [alert addSubview:picker]; // Adjust the alerts bounds alert.bounds = CGRectMake(0, 0, 320 + 20, alert.bounds.size.height + 216 + 20); 

EDIT

As noted by both comments and new answers to many of the questions “How to add some questions to UIAlertView ”, this method no longer works in iOS7 and higher. This really should be taken as evidence that this is a fundamentally bad idea.

Also note the use of setValue:forKey: in the common "solution" for the "problem" due to the inability to change the hierarchy of Apple's private view. setValue:forKey: is one of those methods that attracts the attention of private API scanners. Look for an accessoryView search in the UIAlertView documentation and headers. This is not in the docs. The only related element in the headers is ivar named _accessoryView , which is marked as @private .

Again for a native or private application, I suppose everything is fine, but still.

+3
source

In Swift:

  let myDatePicker: UIDatePicker = UIDatePicker() // setting properties of the datePicker myDatePicker.timeZone = NSTimeZone.localTimeZone() myDatePicker.frame = CGRectMake(0, 15, 270, 200) let alertController = UIAlertController(title: "\n\n\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertControllerStyle.Alert) alertController.view.addSubview(myDatePicker) let somethingAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil) let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil) alertController.addAction(somethingAction) alertController.addAction(cancelAction) self.presentViewController(alertController, animated: true, completion:{}) 
+4
source

You will not like this answer, but my suggestion: do not do this! UIAlertView is not intended for you to add your own views; examples that you find people who do this are examples of misconduct. Create your own view containing the picker, text, buttons, etc. that you want, and present it in some way. For example, you can present it in text through your own view controller, for example. with presentModalViewController:animated: or presentViewController:animated:completion: If you are on an iPad, you can use popover (this can be modal if it's important).

+2
source

This is what worked for me (using iOS7). Call this before [alertView show] :

 [alertView setValue:customContentView forKey:@"accessoryView"]; 

Example:

 UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"TEST" message:@"subview" delegate:nil cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil]; UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)]; v.backgroundColor = [UIColor yellowColor]; [av setValue:v forKey:@"accessoryView"]; [av show]; 

Source: iOS 7 UIDatePicker in UIAlertView setup

+2
source

This works for me on ios7 and above.

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Select Date" message:@"" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; UIDatePicker *picker = [[UIDatePicker alloc] initWithFrame:CGRectMake(10, alert.bounds.size.height, 320, 216)]; [alert addSubview:picker]; alert.bounds = CGRectMake(0, 0, 320 + 20, alert.bounds.size.height + 216 + 20); [alert setValue:picker forKey:@"accessoryView"]; [alert show]; 
+2
source

Swift 5 answer version @Matheus Domingos

  let myDatePicker: UIDatePicker = UIDatePicker() myDatePicker.timeZone = NSTimeZone.local myDatePicker.frame = CGRect(x: 0, y: 15, width: 270, height: 200) let alertController = UIAlertController(title: "\n\n\n\n\n\n\n\n", message: nil, preferredStyle: UIAlertController.Style.alert) alertController.view.addSubview(myDatePicker) let somethingAction = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil) let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertAction.Style.cancel, handler: nil) alertController.addAction(somethingAction) alertController.addAction(cancelAction) present(alertController, animated: true, completion:{}) 
0
source

All Articles