objective-c - 在 UIImageView 中居中 UIActivityIndi​​catorView

标签 objective-c xcode uiimageview uiactivityindicatorview

我创建了 UIImageView 的自定义子类,并且在一种方法中,我想在加载图像时向中心添加一个事件指示器。我正在使用以下代码,但事件监视器位于 UIImageView 之外。我该怎么做才能使它恰好在 UIImageView 的范围内居中?

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

indicator.autoresizingMask =
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleLeftMargin;

indicator.center = self.center;

[self addSubview:indicator];    
[indicator startAnimating];

最佳答案

center 属性是 View 在其 superview 坐标空间中的坐标。因此,您需要在 imageView 的坐标空间中设置指示器的坐标。正确的方法是:

indicator.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));

关于objective-c - 在 UIImageView 中居中 UIActivityIndi​​catorView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10781291/

相关文章:

objective-c - 我应该如何将对象实例导入核心数据?

ios - NSMutableArray 在 for 循环中将对象设置为 null

iphone - 在 UIImageView 中使用标准 iOS 图像?

swift - Xcode升级后Core Data出现数据丢失

iphone - UIImageView 旋转不正确?

ios - 图片查看动画

ios - 如何在 Flutter 中读取已使用 iOS 中的 NSKeyedArchiver 写入文件系统的文件作为字符串?

iphone - IOS项目中的C++前向引用(前向引用 'enum'类型)

ios - 使用 swift 连接 UIImageView 以查看 Images.xcassets 中的 Controller 显示图像

iphone - 如何将代码附加到正在运行的 iPhone 应用程序