【问题标题】:How can I show alertview with activity indicator?如何显示带有活动指示器的警报视图?
【发布时间】:2011-06-21 20:38:30
【问题描述】:

我想显示带有消息的警报视图:“正在加载数据”和旋转活动指示器。我该怎么做?

【问题讨论】:

    标签: ios iphone uialertview uiactivityindicatorview


    【解决方案1】:

    注意:此解决方案不适用于 iOS 7 及更高版本。

    这是我的看法:

    alertView = [[UIAlertView alloc] initWithTitle:@"Submitting Entry"
                                           message:@"\n"
                                          delegate:self
                                 cancelButtonTitle:nil
                                 otherButtonTitles:nil];
    
    UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];   
    spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
    [alertView addSubview:spinner];
    [spinner startAnimating];
    [alertView show];
    

    并在代码中使用:

    [alertView dismissWithClickedButtonIndex:0 animated:YES];
    

    【讨论】:

    • 使用`spinner.center = CGPointMake(0.0, -spinner.frame.size.height); spinner.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;` 始终将微调器定位在警报的底部中心
    • 微调器不显示。我的 alterView 只显示提交条目。我正在使用 iOS 7。
    • @coolcool1994 这还没有在 iOS 7 上测试过。2 年前的回答 ;)
    • 确实如此 :) 我希望我能知道 iOS7 的这种方式 :}
    【解决方案2】:

    这适用于 iOS 7

    addSubView 不适用于 iOS 7 及更高版本的 UIAlertView。试试这个

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

    【讨论】:

    • 这是适用于 iOS 7 的解决方案(针对此评论的当前日期)。
    【解决方案3】:

    您可以添加标签和活动指示器作为警报视图的子视图。你必须做这样的事情

    myAlertView = [[UIAlertView alloc] initWithTitle:@"Loading" message:@"\n\n"
                                            delegate:self
                                   cancelButtonTitle:@""
                                   otherButtonTitles:@"OK", nil];  
    
    UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc]
                    initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];   
    loading.frame=CGRectMake(150, 150, 16, 16);
    [myAlertView addSubview:loading];
    [myAlertView show];
    

    ..在这种情况下最好使用 UIActionSheet...

    【讨论】:

    • 谢谢,我会用 UIActionSheet + UIActivityIndi​​catorView
    • 为什么要在这种情况下使用 UIActionSheet? (我知道 Apple 可能会同意你的看法,但我想知道为什么)。你没有使用任何私有 API,对吧?
    • 你错过了[loading startAnimating],而且还有空按钮显示。
    • 你会错过 [loading startAnimating];我猜
    【解决方案4】:

    您可以添加标签和活动指示器作为警报视图的子视图。你必须做这样的事情......

    - (IBAction)showAlertWithActivity:(id)sender{
    
    alerta = [[UIAlertView alloc] initWithTitle:@"Guardando datos..."
                                                message:@"\n"
                                               delegate:self
                                      cancelButtonTitle:nil
                                      otherButtonTitles:nil];
    
            UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
            spinner.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
            [alerta addSubview:spinner];
            [spinner startAnimating];
            [alerta show];
    
    
            [self performSelector:@selector(close) withObject:self afterDelay:1];
    
    
        }
        -(void)close{
    
            [alerta dismissWithClickedButtonIndex:0 animated:YES];
    
        }
    

    【讨论】:

      【解决方案5】:

      在你的 .h 文件中添加这个 UIAlertView *connectingAlert;

      并将这两个函数添加到您的 .m 文件中

      //show loading activity.
      - (void)startSpinner:(NSString *)message {
          //  Purchasing Spinner.
          if (!connectingAlert) {
              connectingAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(message,@"")
                                                       message:nil
                                                      delegate:self
                                             cancelButtonTitle:nil
                                             otherButtonTitles:nil];
              connectingAlert.tag = (NSUInteger)300;
              [connectingAlert show];
      
              UIActivityIndicatorView *connectingIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
              connectingIndicator.frame = CGRectMake(139.0f-18.0f,50.0f,37.0f,37.0f);
              [connectingAlert addSubview:connectingIndicator];
              [connectingIndicator startAnimating];
      
          }
      }
      //hide loading activity.
      - (void)stopSpinner {
          if (connectingAlert) {
              [connectingAlert dismissWithClickedButtonIndex:0 animated:YES];
              connectingAlert = nil;
          }
          // [self performSelector:@selector(showBadNews:) withObject:error afterDelay:0.1];
      }
      

      然后调用

      [self startSpinner:@"Your message........"];
      [self stopSpinner];
      

      【讨论】:

        【解决方案6】:

        在 Swift 3 中

        let loadingAlertController = UIAlertController(title: "Loading", message: nil, preferredStyle: .alert)
        
        let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
        activityIndicator.translatesAutoresizingMaskIntoConstraints = false
        
        loadingAlertController.view.addSubview(activityIndicator)
        
        let xConstraint = NSLayoutConstraint(item: activityIndicator, attribute: .centerX, relatedBy: .equal, toItem: loadingAlertController.view, attribute: .centerX, multiplier: 1, constant: 0)
        
        let yConstraint = NSLayoutConstraint(item: activityIndicator, attribute: .centerY, relatedBy: .equal, toItem: loadingAlertController.view, attribute: .centerY, multiplier: 1.4, constant: 0)
        
        NSLayoutConstraint.activate([ xConstraint, yConstraint])
        
        activityIndicator.isUserInteractionEnabled = false
        activityIndicator.startAnimating()
        
        let height: NSLayoutConstraint = NSLayoutConstraint(item: loadingAlertController.view, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.notAnAttribute, multiplier: 1, constant: 80)
        
        loadingAlertController.view.addConstraint(height);
        
        self.present(loadingAlertController, animated: true, completion: nil)
        

        【讨论】:

          【解决方案7】:
          UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alarm" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
          
          UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc]
                                              initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];   
          loading.frame=CGRectMake(125, 50, 36, 36);
          [loading startAnimating];
          [alert addSubview:loading];
          [alert show];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-12-12
            • 1970-01-01
            • 1970-01-01
            • 2016-06-25
            • 1970-01-01
            • 2023-04-07
            相关资源
            最近更新 更多