【问题标题】:Adding UIActivityindicatorView in an UIAlertView在 UIAlertView 中添加 UIActivityindicatorView
【发布时间】:2009-06-02 18:53:19
【问题描述】:

我想创建一个 UIAlertView,它会说“...进行中”。它还会显示 UIActivityindicatorView 就可以了。你能告诉我该怎么做吗?

谢谢。

【问题讨论】:

    标签: iphone


    【解决方案1】:

    它非常简单。只需创建一个 UIActivityIndi​​catorView 并将其作为子视图添加到 UIAlertView。

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" " message:@" " 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];
    

    【讨论】:

    • 这是一个很好的答案,但我想知道如果没有按钮,我怎么能从视图中将其关闭?
    • [alert dismissWithClickedButtonIndex:0 animated:YES];
    • 这不是苹果禁止的吗? (向 UIAlertView 添加子视图)
    • 我不知道。它不使用任何私有 API。很久以前,我已经批准了这段代码。
    • 对于iOS7,无法将子视图添加到UIAlertView!参考this
    【解决方案2】:

    在我的情况下,我发现使用硬编码的帧来源很糟糕。如果我的消息多行,指示器会显示在我的消息顶部。

    所以我创建带有布局指示器的函数,如果大小为 UIAlertView

    +(UIAlertView*) progressAlertWithTitle:(NSString*) title andMessage:(NSString*) message andDelegate:(id)delegate{
        UIAlertView *progressAlert = [[UIAlertView alloc] init];
        [progressAlert setTitle:title];
        [progressAlert setMessage:message];
        [progressAlert setDelegate:delegate];
        UIActivityIndicatorView *progress=nil;
        progress= [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    
        [progressAlert addSubview:progress];
        [progress startAnimating];
    
        [progressAlert show];
    
        progress.frame=CGRectMake(progressAlert.frame.size.width/2-progress.frame.size.width, progressAlert.frame.size.height-progress.frame.size.height*2, progress.frame.size.width, progress.frame.size.height);
    
    
        return progressAlert;
    }
    

    在这种情况下,指示器总是按中心

    一行消息:

    多一行留言:

    【讨论】:

      【解决方案3】:
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
      UIActivityIndicatorView   *spinner = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
      spinner.center = CGRectMake(xcords, ycords, width, height);
      [alert addSubview:spinner];
      [spinner startanimating];
      [alert show];
      

      此微调器在关闭 AlertView 时被隐藏。

      [alert dismissWithClickedButtonIndex:0 animated:YES];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-29
        相关资源
        最近更新 更多