senty senty - 7 months ago 54
Swift Question

Setting Activity Indicator to CollectionViewCell

I am trying to display activity indicator for the initial load time for each collection view cell. I tried placing it in

cellForItemAtIndexPath
however, it's getting created 20 pixels above the cell for some reason.

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = self.collectionView.dequeueReusableCellWithReuseIdentifier("AACollectionViewCell", forIndexPath: indexPath) as! AACollectionViewCell

let activityView = UIActivityIndicatorView(activityIndicatorStyle: .Gray)

activityView.center = cell.theImage.center
// Also tried
activityView.center = cell.contentView.center

activityView.startAnimating()

return cell
}


What is the proper way of handling it?




Edit: I also tried this but it's not creating activityIndicator anywhere

let activityView = UIActivityIndicatorView(activityIndicatorStyle: .Gray)
activityView.center = cell.contentView.center
cell.contentView.addSubview(activityView)

Answer

cell.contentView.center gives you the frame of contentView from its superview's coordinate system. What you should do:

activityView.center = CGPointMake(cell.contentView.frame.size.width / 2, cell.contentView.frame.size.height / 2)
Comments