【问题标题】:Alert view is showing white rectangle in iOS7警报视图在 iOS7 中显示白色矩形
【发布时间】:2013-09-24 13:30:48
【问题描述】:

以下代码在 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。如您所见,即使尺寸更小。看起来它没有完全初始化或什么的。

【问题讨论】:

    标签: ios ios7


    【解决方案1】:

    现在addSubview 在iOS7 中的UIAlertView 中不可用

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

    您也可以使用SVProgressHUD

    【讨论】:

    • 我不认为这是活动指示器的问题。请检查我的更新。谢谢
    • 这是真的。 Apple 已故意阻止 addSubview 发出警报。
    • 你有那个声明的链接吗?
    • @Keab42 参见UIAlertView Class Reference“UIAlertView 类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不得修改。”跨度>
    【解决方案2】:

    我必须很快解决这个问题,因此我构建了一个带有动画的 iOS7 UIAlertView 风格的 UIView,可以使用任何自定义内容进行扩展。

    可能会有其他人可以使用我的解决方案,所以我创建了the whole code available on Github

    另外,如果您想在以前的操作系统版本下继续使用 UIAlertView,您必须分叉代码。这可能和听起来一样糟糕,我正在使用以下内容:

    float sysVer = [[[UIDevice currentDevice] systemVersion] floatValue];
    
    if (sysVer < 7) {
      // your old solution
    } else {
      .. // iOS7 dialog code
    }
    

    (请注意,这绝不是一个真正的解决方案 - 如果 Apple 不希望我们将对话框用于各种事情,那么我们可能不应该。)

    【讨论】:

    • 可以无按钮吗?
    • @RobCroll 当然,我不小心留下了默认的 xcode 内容。随意使用它,稍后会尽快更新文件。
    • 感谢您的努力。但是,这仍然是“错误的”。如果您尝试将 CustomIOS7AlertView 添加到 tableview,它仍然允许用户向下滚动 tableView。使用时:CustomIOS7AlertView *alertView = [[CustomIOS7AlertView alloc] initWithParentView:self.superview];然后层次结构都搞砸了(即 tableview 标题出现在 alertView 上方。不过感谢您的帮助......在我看来,坚持使用 TSAlertView,直到他们决定更新到 IOS7....
    • @GeorgeAsda 确保在 initWithParentView:view 方法中传递正确的视图。如果它是一个 tableview,UIView 将被附加到那里 - 也许你想传递视图控制器的视图。
    • @Wimagguc 我正在尝试通过 uitableview 单元格传递它
    【解决方案3】:

    从 iOS 7 开始,您可以:

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

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

    【讨论】:

    • 太棒了!这对我有用。无需实现那些花哨的自定义东西。
    • 唯一的事情是你必须在[alertView show]之前调用它。
    • 这听起来像是将来可能行不通的事情。
    • 它不是一个公共 API,但它被应用商店中的应用所接受。您可以说它与 OP 提到的使用 addSubView 一样有效,它一直有效到 iOS 7。但是,由于这是 Apple 在 iOS7 中引入的,我猜 Apple 自己的应用程序在显示的不仅仅是文本时会使用它在警报中。
    • 很棒的答案!这比上面的解决方案好得多(这也可以工作,但这要容易得多)。这需要更多的投票。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多