在其图层上居中 Activity 指示器视图

Centering Activity Indicator view on its layer

我在网上搜索也没有解决这个问题。我有一个 Activity 指示器视图,其中包含一个我想自定义的图层。更具体地说,我想增加它的大小。问题是 activity 指标不再以其背景为中心。

这应该是相当简单的事情,但我不知道该怎么做。管理层似乎比添加其他子视图更简洁。

这是我使用下面的代码得到的结果。

UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc]
                                             initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [activityView setColor:[UIColor whiteColor]];
    activityView.layer.frame = CGRectMake(self.view.bounds.size.width/2-30, self.view.bounds.size.height/2-30, 60, 60);
    activityView.layer.cornerRadius = 5;
    [activityView.layer setBackgroundColor:[[UIColor colorWithRed:2/255.0 green:79/255.0 blue:105/255.0 alpha:0.9] CGColor]];

    activityView.center = self.view.center;
    //activityView.center = also tried to center on layer, layer.contentsRect,... without any success;
    [activityView startAnimating];
    [self.view addSubview:activityView];

如果有人能帮助我...

如果你想让 activity 指标变大,试试这个

activityView.transform = CGAffineTransformMakeScale(1.75, 1.75);

并且不要忘记添加 #import <QuartzCore/QuartzCore.h>

因此您的代码应如下所示:

UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc]
                                             initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [activityView setColor:[UIColor whiteColor]];
    activityView.layer.cornerRadius = 5;
    [activityView.layer setBackgroundColor:[[UIColor colorWithRed:2/255.0 green:79/255.0 blue:105/255.0 alpha:0.9] CGColor]];

    activityView.transform = CGAffineTransformMakeScale(1.75, 1.75);

    activityView.center = self.view.center;
    [activityView startAnimating];
    [self.view addSubview:activityView];

希望对您有所帮助

你可以用它来居中 activity:

activityView.subviews[0].center = CGPointMake(CGRectGetMidX(activityView.bounds), CGRectGetMidY(activityView.bounds));

但我认为这是不好的方式。尝试制作视图并将 Activity 添加到此视图而不更改其框架。