ios - 在其图层上居中事件指示器 View

标签 ios objective-c calayer centering uiactivityindicatorview

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

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

这是我通过下面的代码得到的结果。

background layer is centered but not the activity indicator

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];

如果有人能帮助我...

最佳答案

您可以使用它来居中事件:

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

但我认为这是不好的方式。尝试在不改变其框架的情况下制作 View 并将 Activity 添加到该 View 。

关于ios - 在其图层上居中事件指示器 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42781902/

相关文章:

ios - iPhone A13 Bionic 破解 Streaming SDK(编码)

ios - Cocoapods Podspec 源 HTTP 身份验证支持

ios - 自定义类型右栏按钮项之间的间距

ios - 调用drawRect在CALayer之上进行绘制?

ios - 如何禁用 CALayer 隐式动画?

ios - 多次调用 textFieldShouldEndEditing

ios - 为什么我使用NSSortDescriptor的排序方法效果不佳?

iphone - iOS - 连接后端 API 服务器时出现 3G 连接速度慢的问题

iphone - 在编辑模式下启用 UITableViewCell 中的所有文本字段

objective-c - 不透明度渐变应用于 CALayer?