【发布时间】:2015-07-14 13:38:37
【问题描述】:
我们将不再使用 MBProgressHUD,因为它在我们的应用中过于故障,并且没有阻止用户输入或提供取消按钮等功能。
所以,我尝试实现Swipesight's How to display activity indicator in center of 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 保留空间,然后如何实际放置它在那里?
【问题讨论】:
-
有一个 POD 可以满足您的需求。此 POD 在社区中广为人知,易于使用并被大量应用程序使用。它是麻省理工学院的许可证。我觉得你应该试试。 MBProgressHUD
-
事实上,我正在尝试这样做,因为我们正在远离 MBProgressHUD。它在我们的应用程序中太小了,并且没有阻止用户输入或提供取消按钮等功能。
标签: ios objective-c uiactivityindicatorview uialertcontroller