Alert view is showing white rectangle in iOS7

我与影子孤独终老i 提交于 2019-12-28 02:52:27

问题


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.

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


回答1:



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.




回答2:


From iOS 7 onwards, you can do:

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

to get custom content in a standard alert view.




回答3:


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.

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.)



来源:https://stackoverflow.com/questions/18895106/alert-view-is-showing-white-rectangle-in-ios7

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!