1

作为标题,我想在 UIAlertView 实例的中心添加一个 UIActivityIndi​​catorView 实例。这是我的代码:

    alertView  = [[UIAlertView alloc]initWithTitle:@"Processing" message:@"" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];

    CGRect screenRect = [[UIScreen mainScreen] bounds];
indicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(screenRect.size.width/2, screenRect.size.height/2,50,50)];
[alertView addSubview: indicator];
[indicator startAnimating];
[alertView show];

我看到的只是警报视图。我犯了什么错误吗?

4

3 回答 3

11

在设置警报视图框架中的 x 和 y 位置时,您忘记考虑指示器的宽度和高度。

indicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(roundf((screenRect.size.width - 50) / 2), roundf((screenRect.size.height - 50) / 2),50,50)];

编辑:这正是我通常使用的那个:(不要忘记释放东西等)

UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Loading..." message: nil delegate:self cancelButtonTitle: nil otherButtonTitles: nil];
UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame: CGRectMake(125, 50, 30, 30)];
    progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[alert addSubview: progress];
[progress startAnimating];
[alert show];
于 2013-03-13T16:46:42.730 回答
3

IOS 7+

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Loading data" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[indicator startAnimating];

[alertView setValue:indicator forKey:@"accessoryView"];
[alertView show];

解雇

[alertView dismissWithClickedButtonIndex:0 animated:YES];
于 2014-10-07T02:56:28.090 回答
0
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Loading..." message: nil delegate:self cancelButtonTitle: nil otherButtonTitles: nil];
UIActivityIndicatorView *progress= [[UIActivityIndicatorView alloc] initWithFrame: CGRectMake(125, 50, 30, 30)];
    progress.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[alert addSubview: progress];
[progress startAnimating];
[alert show];
于 2013-12-24T12:38:17.243 回答