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

时间:2015-07-14 13:38:38

标签: ios objective-c uiactivityindicatorview uialertcontroller

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

所以,我试图实施Swipesight's How to display activity indicator in center of UIAlertController?,我遇到了指标的不正确定位:

A <code>UIAlertController</code> with "Loading" and censored text in the center of the screen, but the activity indicator far to the left and below the alert controller
它是绿色的,因为我们的应用程序的色调是绿色的。

如您所见,它不在控制器的白色矩形部分,而是灰色背景。这类似于他的“@ 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那样),所以我会需要一种方法为它预留一些空间。

所以,我的问题有两个:如何为UIActivityIndicatorView的白色矩形中的UIAlertController预留空间,然后如何做我实际上把它放在那里?

1 个答案:

答案 0 :(得分:14)

与此处提及的JonasG一样,有一个名为contentViewController的属性,我们可以使用KVC进行访问

示例:

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

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

所以这里是你的代码应该是什么样的(测试并且工作正常):

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

}
  

您可以覆盖-preferredContentSize以返回自定义尺寸   查看您设置为contentViewController的控制器。

在我们的案例中,它是customVC

结果:

alert

希望文字低于指标吗?

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

contentViewController

我们现在的代码较少:

contentViewController

结果:

Custom alert