serdar aylanc
serdar aylanc

Reputation: 1347

hide header while collection view is loading

I am using NVActivityIndicatorView for loading animation. I have these function to add and remove activity indicator.

func addActivityIndicator() {}
func startActivityIndicatorView() {}
func stopActivityIndicatorView() {}

I have a header which I implement in

override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
   let headerView = ...
   return headerView 
}

My problem is header is visible while collectionView is loading. I want to hide it while collectionView is loading.

Upvotes: 3

Views: 5294

Answers (1)

Ozgur Vatansever
Ozgur Vatansever

Reputation: 52163

You are probably performing some asynchronous operation while the indicator is animating so you should let the collection view know that the operation is finished by calling reloadData so it'll re-layout its UI elements including the headers via viewForSupplementaryElementOfKind:

First off, what you need is to return CGSize.zero from collectionView:layout:referenceSizeForHeaderInSection if indicator is on screen so the header won't be populated:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  if indicatorView.isAnimating { 
    return CGSize.zero
  } else {
    return CGSize(width: collectionView.frame.width, height: 50)
  }
}

Then wherever you hide the activity indicator (probably in the completion block of the asynchronous operation), you should call collectionView.reloadData so viewForSupplementaryElementOfKind will be called again:

// operation is done, refreshing the content..
self.stopActivityIndicatorView()
self.collectionView.reloadData() 
...

Upvotes: 2

Related Questions