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

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

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

<img src="http://sofzh.miximages.com/ios/ehy8BdD.png" alt="一个 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保留空间 ,然后我如何将它放在那里呢?

就像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

结果:

警报

希望文本低于指标吗?

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

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

结果:

自定义提醒