Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 884
serdar aylanc Avatar asked Jun 29 '26 11:06

serdar aylanc


1 Answers

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() 
...
like image 77
Ozgur Vatansever Avatar answered Jul 02 '26 04:07

Ozgur Vatansever



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!