【问题标题】:How to add UIActivityIndicatorView at center of UIAlertView?如何在 UIAlertView 的中心添加 UIActivityIndi​​catorView?
【发布时间】:2013-03-13 16:42:58
【问题描述】:

作为标题,我想在 UIAlertView 实例的中心添加一个 UIActivityIndi​​catorView 实例。这是我的代码:

    alertView  = [[UIAlertView alloc]initWithTitle:@"Processing" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];

    CGRect screenRect = [[UIScreen mainScreen] bounds];
indicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(screenRect.size.width/2, screenRect.size.height/2,50,50)];
[alertView addSubview: indicator];
[indicator startAnimating];
[alertView show];

我看到的只是 alertView。我有没有搞错?

【问题讨论】:

    标签: ios uiactivityindicatorview alertview


    【解决方案1】:

    在设置警报视图框架中的 x 和 y 位置时,您忘记考虑指示器的宽度和高度。

    indicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(roundf((screenRect.size.width - 50) / 2), roundf((screenRect.size.height - 50) / 2),50,50)];
    

    编辑:这正是我通常使用的那个:(不要忘记释放东西等)

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Loading..." message: nil delegate:self cancelButtonTitle: nil otherButtonTitles: nil];
    UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame: CGRectMake(125, 50, 30, 30)];
        progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
    [alert addSubview: progress];
    [progress startAnimating];
    [alert show];
    

    【讨论】:

    • indicator设置center属性不是更简单吗?
    • 我试过这个,但没有帮助。当我设置indicator.center = alertView.center;时,指示器总是显示在alertview的左上角
    • @Scott 也许,这是一个品味问题。 :) @Nguyen 我已经编辑了我的答案...你设置了指标视图的样式吗?
    • 太棒了。你的编辑有帮助。但我必须再等 4 分钟才能接受这个答案。谢谢一堆。 :)
    • 我将把它留在这里,以防有人需要以编程方式从另一个方法中解除它:[alert dismissWithClickedButtonIndex:0 animated:YES];
    【解决方案2】:

    适用于 IOS 7+

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Loading data" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [indicator startAnimating];
    
    [alertView setValue:indicator forKey:@"accessoryView"];
    [alertView show];
    

    然后解雇

    [alertView dismissWithClickedButtonIndex:0 animated:YES];
    

    【讨论】:

      【解决方案3】:
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Loading..." message: nil delegate:self cancelButtonTitle: nil otherButtonTitles: nil];
      UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame: CGRectMake(125, 50, 30, 30)];
          progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
      [alert addSubview: progress];
      [progress startAnimating];
      [alert show];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-15
        • 1970-01-01
        • 2016-08-31
        • 1970-01-01
        相关资源
        最近更新 更多