How do I put a UIActivityIndicatorView in a UIAlertController?

后端 未结 1 799
庸人自扰
庸人自扰 2021-01-06 08:28

We\'re moving away from MBProgressHUD because it\'s too glitchy in our app, and doesn\'t have features such as blocking user input or providing a Cancel button.

So,

相关标签:
1条回答
  • 2021-01-06 09:24

    Like JonasG mentioned here there is a property named contentViewController and we can use KVC for access

    Example :

    UIViewController *v = [[UIViewController alloc] init];
    v.view.backgroundColor = [UIColor redColor];
    
    [alertController setValue:v forKey:@"contentViewController"];
    

    So here is how your code should looks like (tested and works fine) :

    - (IBAction)buttonClicked:(id)sender
    {
    
        self.alertController = [UIAlertController alertControllerWithTitle: @"Loading"
                                                               message: nil
                                                        preferredStyle: UIAlertControllerStyleAlert];
    
    
        [self.alertController addAction:[UIAlertAction actionWithTitle: @"Cancel" style: UIAlertActionStyleCancel handler:nil]];
    
    
        UIViewController *customVC     = [[UIViewController alloc] init];
    
    
        UIActivityIndicatorView* spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        [spinner startAnimating];
        [customVC.view addSubview:spinner];
    
    
        [customVC.view addConstraint:[NSLayoutConstraint
                               constraintWithItem: spinner
                               attribute:NSLayoutAttributeCenterX
                               relatedBy:NSLayoutRelationEqual
                               toItem:customVC.view
                               attribute:NSLayoutAttributeCenterX
                               multiplier:1.0f
                               constant:0.0f]];
    
    
    
        [customVC.view addConstraint:[NSLayoutConstraint
                               constraintWithItem: spinner
                               attribute:NSLayoutAttributeCenterY
                               relatedBy:NSLayoutRelationEqual
                               toItem:customVC.view
                               attribute:NSLayoutAttributeCenterY
                               multiplier:1.0f
                               constant:0.0f]];
    
    
        [self.alertController setValue:customVC forKey:@"contentViewController"];
    
    
        [self presentViewController: self.alertController
                             animated: true
                           completion: nil];
    
    }
    

    You can override -preferredContentSize to return a custom size in the view controller that you are setting as contentViewController.

    in our case it's customVC

    Result:

    alert

    Want the text to be below the indicator ?

    I have created a UIViewController with an xib to act as a custom controller for our contentViewController in the first example we have created the view controller without xib file, Now we can add views using interface builder, I have added an Activity indicator and set the constraints to be centered horizontally and vertically and a label under the activity indicator which is centered horizontally here is my interface builder:

    contentViewController

    we have less code now:

    - (IBAction)buttonClicked:(id)sender
    {
    
        self.alertController = [UIAlertController alertControllerWithTitle: nil
                                                               message: nil
                                                        preferredStyle: UIAlertControllerStyleAlert];
    
        MyCustomViewController *v     = [[MyCustomViewController alloc] init];
    
        [self.alertController setValue:v forKey:@"contentViewController"];
        [self presentViewController: self.alertController animated: true  completion: nil];
    
    }
    

    Result :

    Custom alert

    0 讨论(0)
提交回复
热议问题