0

The Ok button is not working. I know it happened because I added backgroundImageView. How can i make it work?

This is my code:

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

UIImageView *backgroundImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"clock"]];

backgroundImageView.frame = CGRectMake(0, 0, 282, 200);

backgroundImageView.contentMode = UIViewContentModeScaleToFill;
backgroundImageView.userInteractionEnabled = YES;

[alert addSubview:backgroundImageView];

[alert sendSubviewToBack:backgroundImageView];

[alert show];

4 Answers4

2

Unfortunately Apple will not allow you to add subviews or modify the view hierarchy. See https://developer.apple.com/library/ios/documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html for:

The UIAlertView class is intended to be used as-is and does not support subclassing. The view hierarchy for this class is private and must not be modified.

And see https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/UIKitUICatalog/UIAlertView.html#//apple_ref/doc/uid/TP40012857-UIAlertView for:

Appearance of Alert Views

You cannot customize the appearance of alert views.

Going against these will get your App rejected in the Apple Review process - so I would go back and look at your design of your app.

Community
  • 1
  • 1
Popeye
  • 11,839
  • 9
  • 58
  • 91
2

Apple will not allow you to add subviews to UIAlertView. Subclassing UIAlertView is not an option, this class is not intended to be subclassed and doing so might be a reason of app rejection.

You can use a library from here for custom UIAlertView

Dragos
  • 1,050
  • 16
  • 21
1

Don't add subviews to UIAlertView, it is not supported functionality. If you need to create custom views, find a 3rd parts alert view solution which supports what you need and use that instead.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • 1
    just simple correction from iOS 7 onwards we can't customize standard alert view. – Bhumeshwer katre Dec 16 '13 at 13:00
  • @Bhumeshwerkatre, it was never supported functionality and it should never have been done - this isn't new to iOS7, it just doesn't work at all in iOS7. – Wain Dec 16 '13 at 13:08
  • @Wain whilst I do agree that it shouldn't have been allowed and it can't be done at all now. Apple sometimes would allow an app to slip through the process I have an old app that did this. But now this certainly will not happen. – Popeye Dec 16 '13 at 13:16
  • @Popeye, indeed, I know of several that use this loophole. From Apples point of view it was hard to review as there was a 3rd party implementation for iOS6 that looks basically identical to the built in version. – Wain Dec 16 '13 at 13:29
1

Apple does not recommend adding subviews to UIAlertView You will have to create your own alert view.

Nameet
  • 650
  • 8
  • 20