Ios 如何在UIAlertController中放置UIActivityIndicator视图?

Ios 如何在UIAlertController中放置UIActivityIndicator视图?,ios,objective-c,uiactivityindicatorview,uialertcontroller,Ios,Objective C,Uiactivityindicatorview,Uialertcontroller,我们正在远离MBProgressHUD,因为它在我们的应用程序中太容易出错,并且没有阻止用户输入或提供取消按钮等功能 因此,我试图实施,但我遇到了指示器的不正确定位: UIAlertController,屏幕中央显示“加载”和经过审查的文本,但活动指示器位于警报控制器的左侧和下方“> 它是绿色的,因为我们的应用程序的色调是绿色的 如你所见,它不在控制器的白色矩形部分,而是灰色背景。这使用了类似于他的“@62Shark”解决方案的东西: // in implementation: @proper

我们正在远离MBProgressHUD,因为它在我们的应用程序中太容易出错,并且没有阻止用户输入或提供取消按钮等功能

因此,我试图实施,但我遇到了指示器的不正确定位:

UIAlertController,屏幕中央显示“加载”和经过审查的文本,但活动指示器位于警报控制器的左侧和下方“>
它是绿色的,因为我们的应用程序的色调是绿色的

如你所见,它不在控制器的白色矩形部分,而是灰色背景。这使用了类似于他的“@62Shark”解决方案的东西:

// in implementation:

@property (nonatomic, strong) UIActivityIndicatorView *spinner;
@property (nonatomic, strong) UIAlertController *alertController;


// in init:

_alertController = [UIAlertController alertControllerWithTitle: @"Loading"
                                                         message: nil
                                                  preferredStyle: UIAlertControllerStyleAlert];
_spinner = [UIActivityIndicatorView new];
_spinner.translatesAutoresizingMaskIntoConstraints = false;
_spinner.userInteractionEnabled = false;
_spinner.color = [ThemingAssistant tintColor];
_spinner.frame = _alertController.view.bounds;
_spinner.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[_spinner startAnimating];
[_alertController.view addSubview: _spinner];


// ...

- (void) showOn: (UIViewController *)target
          title: (NSString *)title
        message: (NSString *)message
      canCancel: (BOOL)canCancel
{
    self.alertController.title = title;
    self.alertController.message = message;

    if (canCancel)
    {
        [self.alertController addAction:[UIAlertAction actionWithTitle: @"Cancel"
                                                                 style: UIAlertActionStyleCancel
                                                               handler: ^(UIAlertAction *name){
                                                                   [self customDismiss];
                                                               }]];
    }

    NSDictionary *views = @{@"pending"   : self.alertController.view,
                            @"indicator" : self.spinner};
    NSArray *constraints =
    [NSLayoutConstraint constraintsWithVisualFormat: @"V:[indicator]-(-50)-|"
                                            options: 0
                                            metrics: nil
                                              views: views];
    [constraints arrayByAddingObjectsFromArray:
     [NSLayoutConstraint constraintsWithVisualFormat: @"H:|[indicator]|"
                                             options: 0
                                             metrics: nil
                                               views: views]];


    [target presentViewController: self.alertController
                         animated: true
                       completion: nil];
}
即使这是在白色矩形中,我担心它可能会遇到文本(同样,当我把它放在那里时,我希望它位于中间顶部,就像MBProgressHUD一样),所以我需要一种方法为它保留一些空间

因此,我的问题有两个:如何在
UIAlertController
的白色矩形中为
UIActivityIndicatorView
预留空间,然后如何实际将其放置在其中?

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

示例:

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

[alertController setValue:v forKey:@"contentViewController"];
下面是您的代码的外观(经过测试,工作正常):

您可以覆盖
-preferredContentSize
以在 查看您设置为
contentViewController
的控制器

在我们的例子中,它是
customVC

结果:

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

[alertController setValue:v forKey:@"contentViewController"];

希望文本位于指示器下方吗?

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

[alertController setValue:v forKey:@"contentViewController"];
我创建了一个带有
xib
UIViewController
,作为
contentViewController
的自定义控制器。在第一个示例中,我们创建了没有xib文件的视图控制器,现在我们可以使用界面生成器添加视图,我添加了一个活动指示器,并将约束设置为水平居中N纵向和纵向,活动指示器下方的标签水平居中,这是我的界面生成器:

我们现在的代码更少了:

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

}
结果:

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

[alertController setValue:v forKey:@"contentViewController"];

有一个播客可以满足您的需求。这个播客在社区中很有名,易于使用,并被大量应用程序使用。它是MIT许可证。我认为您应该尝试一下。事实上,我尝试这样做是因为我们正在远离MBProgressHUD。它在我们的应用程序中太容易出错,并且没有阻止用户输入或提供一个取消按钮。谢谢!当它回到我的队列时,我会试试这个。它现在漂亮地位于白色矩形内,但在底部。我如何将它放在文本上方?此外,指示器周围的填充物很大(你的看起来是齐平的,但我的四周大约有半英寸)即使在设置了
preferredContentSize
谢谢你,漂亮!我会在我的新设计中使用它是的,当然,你可以从代码中看到,我删除了
addAction
行,只是为了设计目的;)