15

下面的代码从 iOS 5 到 6.1 都能完美运行。我什至在商店中有带有该代码的应用程序:

-(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];
    }
}

它将显示没有按钮的警报视图,并带有活动指示器和标签。但是在 iOS7 中,我只能看到白色圆角矩形,没有活动指示器。

从 iOS 5 到 7,我该怎么做才能完成这项工作?

更新:

为了更具描述性,我添加了屏幕截图。下面是 iOS 5 到 6.1 的截图。在那里工作正常。

在此处输入图像描述

以下是iOS7。如您所见,即使尺寸更小。看起来它没有完全初始化或其他东西。

在此处输入图像描述

4

3 回答 3

20

从 iOS 7 开始,您可以执行以下操作:

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

在标准警报视图中获取自定义内容。

于 2013-11-22T14:52:38.927 回答
20

现在在 iOS7 中addSubview不可用UIAlertView

该类UIAlertView旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改

作为替代方案,您可以使用SVProgressHUD.

于 2013-09-19T12:55:07.857 回答
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.)

于 2013-09-20T14:46:32.613 回答