Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add subview inside UIAlertView for iOS 7?

I am having an application on iTunes store which displays some UILabel and UIWebView on UIAlertView. According to session video, addSubView for UIAlertView will not work. They have talked about ContentView. But in the GM Seeds SDK, I could not find that property and there seems to be no other way.

The only thing I can do is to create a custom subclass of UIView and make it work like UIAertView. Can you suggest any other simple solution?

Thanks for the help.

like image 617
Apurv Avatar asked Sep 12 '13 08:09

Apurv


3 Answers

You can really change accessoryView to customContentView in iOS7 (and it seems that in iOS8 as well) UIAlertView

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

Try this code:

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

Remember that you need set custom accessoryView before the call [alertView show]

enter image description here

like image 113
malex Avatar answered Nov 10 '22 02:11

malex


AddSubview is not possible from iOS 7.

The only way is to create a custom UIView subclass which can act as UIAlertView. I could find out few components on Github.

The linked one Custom Alert seems to work well. By putting proper version check one can identify to go for native UIAlertView or CustomAlertView.

like image 23
Apurv Avatar answered Nov 10 '22 00:11

Apurv


Adding a subview to an UIAlertView is different in iOS7 from earlier iOS versions. Try something like this:

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
    [myAlertView setValue:myCustomView forKey:@"accessoryView"]; //works only in iOS7
} else {
    myAlertView.message = @"\n"; //or just add \n to the end of the message (it's a workaround to create space inside the alert view
    [myAlertView addSubview:myCustomView];
}

[myAlertView show]; //show the alert AFTER CUSTOMIZATION  
like image 3
Andra Todorescu Avatar answered Nov 10 '22 00:11

Andra Todorescu