如何在UIAlertView的中心添加UIActivityIndi​​catorView?

Ngu*_*inh 1 uiactivityindicatorview ios alertview

作为标题,我想在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];
Run Code Online (Sandbox Code Playgroud)

我看到的只是alertView.我犯了什么错吗?

Luk*_*uke 11

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

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

编辑:这是我通常使用的一个:(不要忘记发布东西等)

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];
Run Code Online (Sandbox Code Playgroud)