15

The following code works perfectly from iOS 5 to 6.1. I even have applications in store with that code:

-(void)showActivityIndicator
{
    if(!mLoadingView) //
    {
        mLoadingView = [[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
        mLoadingView.tag = kAlertViewTag;
    }

    [mLoadingView show];
}

- (void)willPresentAlertView:(UIAlertView *)alertView
{
    if (alertView.tag == kAlertViewTag)
    {
        UIActivityIndicatorView *actInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
        actInd.frame = CGRectMake(128.0f, 45.0f, 25.0f, 25.0f);
        [alertView addSubview:actInd];
        [actInd startAnimating];

        UILabel *l = [[UILabel alloc]init];
        l.text = NSLocalizedString(@"PRODUCT_PURCHASE_INDICATOR_TITLE", @"Please wait...");
        l.font = [UIFont fontWithName:@"Helvetica" size:16];

        float strWidth = [l.text sizeWithFont:l.font].width;
        float frameWidth = alertView.frame.size.width;
        l.frame = CGRectMake((frameWidth - strWidth)/2, -25, 210, 100);

        l.textColor = [UIColor whiteColor];
        l.shadowColor = [UIColor blackColor];
        l.shadowOffset = CGSizeMake(1.0, 1.0);
        l.backgroundColor = [UIColor clearColor];
        [alertView addSubview:l];
    }
}

It will show alert view without buttons and with activity indicator and label. However in iOS7 I can see only white rounded rectangle, no activity indicator.

What can I do to have this work from iOS 5 to 7?

Update:

To be more descriptive I'm adding screenshots. The following is iOS 5 to 6.1 screenshot. Works fine there.

enter image description here

The following is iOS7. As you can see even the size is smaller. Looks like it's not fully initialized or something.

enter image description here

Pablo
  • 28,133
  • 34
  • 125
  • 215

3 Answers3

20

now addSubview is not available in UIAlertView in iOS7

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

As an alternative you can use SVProgressHUD.

ThomasW
  • 16,981
  • 4
  • 79
  • 106
Nagendra Tripathi
  • 923
  • 12
  • 23
  • I don't think it's activity indicator problem. Please check my update. Thanks – Pablo Sep 19 '13 at 12:59
  • This is true. Apple has intentionally blocked `addSubview` on alerts. – Sulthan Sep 19 '13 at 13:33
  • Do you have a link for that declaration? – Keab42 Sep 25 '13 at 11:28
  • @Keab42 see the [`UIAlertView Class Reference`](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html#//apple_ref/doc/uid/TP40006802) "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." – ThomasW Jan 24 '14 at 01:57
20

From iOS 7 onwards, you can do:

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

to get custom content in a standard alert view.

fishinear
  • 6,101
  • 3
  • 36
  • 84
  • Awesome! This worked for me. No need to implement those fancy custom stuff. – Abdullah Umer Dec 23 '13 at 14:14
  • 1
    Only thing is that you have to call this before `[alertView show]`. – Philippe97 Dec 25 '13 at 04:47
  • This sounds like something that is likely to not work in the future. – ThomasW Jan 24 '14 at 06:15
  • It is not a public API, but it is accepted for apps on the app store. You could say it is just as valid as using addSubView, that the OP mentions, which worked until iOS 7. However, as this was introduced by Apple in iOS7, I am guessing it is used by Apple's own apps when they show more than text in alerts. – fishinear Feb 11 '14 at 14:02
  • Awesome answer! This worked much better than the solutions above (which also would work but this is so much easier). This needs to be up-voted more. Thanks! – user2096580 Feb 21 '14 at 00:56
  • You just can't replicate the system view and all of the parameters without a ton of code. This is a much cleaner alternative. – Abandoned Cart Jul 05 '14 at 16:11
19

I had to fix this problem very quickly, hence I have built an iOS7 UIAlertView-style UIView with its animations, which can be extended with any custom content.

There might be others who can use my solution, so I made the whole code available on Github.

enter image description here

Also, if you want to keep using the UIAlertView under previous OS versions, you have to fork the code. It might be as bad as it sounds, I'm using the following:

float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];

if (sysVer < 7) {
  // your old solution
} else {
  .. // iOS7 dialog code
}

(Please be aware that this is by no means a real solution - if Apple doesn't want us to use the dialogs for all sorts of things, then we probably shouldn't.)

Wimagguc
  • 1,001
  • 1
  • 7
  • 12
  • can it be buttonless? – Pablo Sep 20 '13 at 16:09
  • 3
    @RobCroll sure, i left the default xcode stuff by accident. feel free to use it as it is, ill update the files later asap. – Wimagguc Sep 22 '13 at 09:28
  • thanks for your efforts. however this is still "buggy". if you try to add the CustomIOS7AlertView to a tableview, it still allows the user to scroll down the tableView. When using: CustomIOS7AlertView *alertView = [[CustomIOS7AlertView alloc] initWithParentView:self.superview]; then the hierarchy is all messed up (i.e the tableview header appears above the alertView. Thanks for your help though... In my opinion stick with TSAlertView unti they decide to update to IOS7.... – George Asda Sep 26 '13 at 11:59
  • @GeorgeAsda make sure you pass on the right view in the initWithParentView:view method. If it's a tableview, the UIView will be attached there - perhaps you want to pass the view controller's view instead. – Wimagguc Sep 26 '13 at 15:30
  • @Wimagguc i'm trying to pass this through uitableview cell – George Asda Sep 26 '13 at 16:22
  • @GeorgeAsda: it makes sense then: its superview is still the table view only. DM me here or on twitter and i'll take a look at your code if you want. – Wimagguc Sep 26 '13 at 16:31
  • @Wimagguc nothing fancy with my code. Setting up a custom cell with a button that displays an alertView. - (IBAction)testButton { CustomIOS7AlertView *alertView = [[CustomIOS7AlertView alloc] initWithParentView:self.superview]; // Add some custom content to the alert view [alertView setContainerView:[self createDemoView]]; [alertView show]; } - (UIView *)createDemoView { //as per your example code nothing changes here } – George Asda Sep 26 '13 at 19:18
  • Another fork with another design here : https://github.com/kwent/ios-custom-alertview – Quentin Rousseau Oct 31 '13 at 20:15