2

Guys I was adding UIDatePicker to UIAlertView like this

enter image description here

It was fine with iOS 6 and below now in iOS 7 it comes like this

enter image description here

Any ideas why this happens? Is there a better way of doing this?.Any help is appreciated.

Community
  • 1
  • 1
Satheesh
  • 10,998
  • 6
  • 50
  • 93

5 Answers5

8

You can change accessoryView to any own customContentView in a standard alert view in iOS7

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

Note that you must call this before [alertView show].

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

enter image description here

Exactly in the same way you can add your DatePicker.

malex
  • 9,874
  • 3
  • 56
  • 77
3

There is no fix for this. Adding subviews to a UIAlertView was never supported and in iOS 7 this lead to subviews not showing. Some workarounds have been posted on the Apple Developer Forums, but they could easily break in a future release.

I suggest file a bug report. Many others (including myself) have done this, and the more requests Apple gets for it, the higher its priority becomes.

EDIT: I have written a UIAlertView clone that does allow for adding subviews: SDCAlertView.

Scott Berrevoets
  • 16,921
  • 6
  • 59
  • 80
3

On iOS7, you should use the new custom modal transition support in UIKit using UIModalPresentationCustom and transitioningDelegate.

Using these, you can create a view similar to that of an alert view, but custom, where you could add a date picker.

More information here: https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewControllerTransitioningDelegate_protocol/Reference/Reference.html#//apple_ref/occ/intf/UIViewControllerTransitioningDelegate

Léo Natan
  • 56,823
  • 9
  • 150
  • 195
3

This is my component to support addSubview in AlertView.

CXAlertView - Custom alert-view which allow you to add view as main content.

enter image description here

Chris
  • 428
  • 2
  • 10
1

It is not supported and I dont belive they 'fix this' .. roll your own alert view or use a open source alternative. see also UIAlertView addSubview in iOS7

Community
  • 1
  • 1
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135