如何在UIAlertController中放置UIActivityIndi​​catorView?

Ben*_*ero 4 objective-c uiactivityindicatorview ios uialertcontroller

我们正在离开MBProgressHUD,因为它在我们的应用程序中太麻烦了,并且没有阻止用户输入或提供取消按钮等功能.

那么,我试图在UIAlertController的中心实现Swipesight的如何显示活动指示器?,我遇到了指标的不正确定位:

一个<code>UIActivityIndicatorView</code>在<code>UIAlertController</code>白色矩形中保留空间</strong>,然后<strong>如何将其放在那里?</strong></p></p>
    </div>
  </div>

<div class=

Ala*_*din 15

就像JonasG在这里提到的那样,有一个名为的属性contentViewController,我们可以使用KVC进行访问

示例:

UIViewController *v = [[UIViewController alloc] init];
v.view.backgroundColor = [UIColor redColor];

[alertController setValue:v forKey:@"contentViewController"];
Run Code Online (Sandbox Code Playgroud)

所以这里是你的代码应该如何(测试和工作正常):

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

}
Run Code Online (Sandbox Code Playgroud)

您可以覆盖-preferredContentSize以在您设置的视图控制器中返回自定义大小contentViewController.

在我们的例子中它是 customVC

结果:

警报

希望文本低于指标吗?

我创建了UIViewController一个xib用作我们的自定义控制器,contentViewController在第一个例子中我们创建了没有xib文件的视图控制器,现在我们可以使用界面构建器添加视图,我添加了一个活动指示器并将约束设置为居中水平和垂直,活动指示器下面的水平居中的标签是我的界面构建器:

contentViewController

我们现在有更少的代码:

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

}
Run Code Online (Sandbox Code Playgroud)

结果:

自定义提醒