【发布时间】:2017-07-25 21:05:38
【问题描述】:
我正在尝试在每个集合视图单元格中放置一个 UIActivityIndicatorView,因为它会下载其图像。我让它出现在每个单元格中,但它拒绝居中。它停留在左上角。我怎样才能让它正确地居中?
这是我的做法:
extension UIView {
func showActivityIndicator(onView: UIView, withIndicator: UIActivityIndicatorView) {
withIndicator.frame = CGRect(x: onView.frame.midX - 20, y: onView.frame.midY - 20, width: 40, height: 40)
withIndicator.activityIndicatorViewStyle = .whiteLarge
withIndicator.center = onView.center
onView.addSubview(withIndicator)
withIndicator.startAnimating()
}
}
我在 cellForItemAtIndexPath 中调用该函数,例如:
showActivityIndicator(onView: cell.contentView, withIndicator: activityInd)
但我所做的一切都不会将它从左上角移动。有什么建议吗?
【问题讨论】:
-
为什么要将视图传递给作为
UIView扩展的方法?你应该把它称为cell.contentView.showActivityIndicator(activityInd),方法签名应该是func showActivityIndicator(_ indicatorView: UIActivityIndicatorView)。那么方法中对onView的所有引用都应该替换为self。 -
我发现网上真的很快,但是好点,我改了。谢谢!
标签: ios swift uiview uicollectionview uicollectionviewcell