Apurv
Apurv

Reputation: 17186

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.

Upvotes: 22

Views: 41652

Answers (6)

malex
malex

Reputation: 10096

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

Upvotes: 103

leenyburger
leenyburger

Reputation: 1208

The custom alert view on github linked in the accepted answer is great. However, you cannot launch this directly from the viewdidload method. The enclosing UIView is attached to the app's first window, so you have to wait a split second. I added this code to viewdidload which I found as a closed issue.

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5f * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
      // Open the dialog here
   });

Upvotes: -2

Andra Todorescu
Andra Todorescu

Reputation: 628

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  

Upvotes: 3

Guizmo
Guizmo

Reputation: 1

A solution is to subclass UIAlertView and detect the "_UIModalItemAlertContentView" view. My AlertView had a UITextField, so I used it to detect the content view :

- (void)show
{
    [ super show ];
    UIView *contentView=nil;
    if([[[UIDevice currentDevice] systemVersion] floatValue ] < 7.0f)
    {
        contentView=self;
    } else {
        UIView *rootView=[self textFieldAtIndex:0];
        while((rootView=[rootView superview])!=nil)
        {
            if([ NSStringFromClass([rootView class]) isEqualToString:@"_UIModalItemAlertContentView"])
            {
                contentView=rootView;
                break;
            }
        }
    }

    if(contentView!=nil)
    {
        [ contentView addSubview: ... ];
    }
}

Upvotes: -9

Emon
Emon

Reputation: 462

Think you will get help also if you use it :)

syncProgressBarView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
syncProgressBarView.frame =CGRectMake(20, 55, 250, 20);
[syncProgressBarView setProgress:0.0f animated:NO];
syncProgressBarView.backgroundColor =[UIColor clearColor];
[progressView addSubview:syncProgressBarView];

Making a new view for sub-viewing UIProgressView

progressView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, 100)];
[[UIApplication sharedApplication].keyWindow addSubview:progressView];
progressView.backgroundColor = [UIColor clearColor];
progressView.center = [UIApplication sharedApplication].keyWindow.center;
syncProgressBarView.frame = CGRectMake(progressView.frame.size.width/2 - 250/2, progressView.frame.size.height/2 - 10, 250, 20);
[[UIApplication sharedApplication].keyWindow bringSubviewToFront:[progressView superview]];

One more thing you have to do...

dispatch_async(dispatch_get_main_queue(), ^{
                [self updateProgressBar];
            });

Upvotes: -1

Apurv
Apurv

Reputation: 17186

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.

Upvotes: 10

Related Questions