iOS 中未显示 Spinner(UIActivityIndi​​cator)

Spinner(UIActivityIndicator) doesn't show in iOS

我想在从 url 下载文件时显示微调器。它下载文件但不显示微调器。标签也没有居中。 我怎样才能让它居中。

我已经尝试使用 self.view.center,但没有用。

代码

container = [[UIView alloc] init];
//container.center = self.view.center;
activityLabel = [[UILabel alloc] init];

activityLabel.text = NSLocalizedString(message, @"string1");
activityLabel.textColor = [UIColor darkGrayColor];
activityLabel.font = [UIFont boldSystemFontOfSize:8];
[container addSubview:activityLabel];
activityLabel.frame = CGRectMake(30, 55, 225, 50);

activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[container addSubview:activityIndicator];
activityIndicator.frame = CGRectMake(30, 55, 225, 50);
[self.view addSubview:container];
container.center = CGPointMake(container.frame.size.width/2, container.frame.size.height/2);
self.view.backgroundColor = [UIColor whiteColor];
[activityIndicator startAnimating];

请点亮它....

每次我想显示 activity 指标时,我都会调用此方法。

 - (void) showActivityIndicator{

    self.spinner.hidden=NO;


    [self.spinner startAnimating];

    [self.view bringSubviewToFront:self.spinner];

    [self.view setNeedsDisplay];


    }

这是我的 viewDidLoad 方法

 - (void)viewDidLoad{
        [super viewDidLoad];
       // Do any additional setup after loading the view.

       self.spinner= [[UIActivityIndicatorView alloc]       initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

       self.spinner.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);

       [self.spinner setCenter:self.view.center]; // I do this because I'm in      landscape mode
       [self.view addSubview:self.spinner];

       self.spinner.hidesWhenStopped=YES;

}

我就是这样调用这个函数的。

        [NSThread detachNewThreadSelector:@selector(showActivityIndicator) toTarget:self withObject:nil];

您没有设置容器的框架。我猜你设置了:

activityIndicator.frame = CGRectMake(30, 55, 225, 50);

而不是

container.frame = CGRectMake(30, 55, 225, 50);

要将容器放在中央,请执行以下操作:

container.center=self.view.center;

在您的代码中,您将容器置于其自身的中心。

代码

-(void)startAnimating{
UIView *  container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
container.center = self.view.center;
container.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.4];
container.layer.cornerRadius = 10;
container.layer.masksToBounds = true;
container.tag = 134;
UILabel * activityLabel = [[UILabel alloc] init];
activityLabel.frame = CGRectMake(0, 70, 100, 30);
activityLabel.text = @"Description";
activityLabel.textColor = [UIColor darkGrayColor];
activityLabel.textAlignment = NSTextAlignmentCenter;
activityLabel.font = [UIFont boldSystemFontOfSize:10];
[container addSubview:activityLabel];


UIActivityIndicatorView *  activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.center = CGPointMake(container.frame.size.width/2, container.frame.size.height/2);
[container addSubview:activityIndicator];

[self.view addSubview:container];

[activityIndicator startAnimating];
}

-(void)stopAnimating{
    UIView * containView = [self.view viewWithTag:134];
    [containView removeFromSuperview];
 }

想开始的时候打电话[self startAnimating]

想停止的时候调用[self stopAnimating]

截图