【问题标题】:hide header while collection view is loading加载集合视图时隐藏标题
【发布时间】:2017-04-29 14:45:28
【问题描述】:

我正在使用NVActivityIndicatorView 加载动画。 我有这些功能来添加和删除活动指示器。

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

我有一个我在

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

我的问题是在加载 collectionView 时标题可见。我想在 collectionView 加载时隐藏它。

【问题讨论】:

    标签: swift header collectionview


    【解决方案1】:

    您可能在指示器动画时执行了一些异步操作,因此您应该通过调用reloadData 让集合视图知道操作已完成,这样它将通过viewForSupplementaryElementOfKind 重新布局包括标题在内的 UI 元素:

    首先,如果指示器在屏幕上,您需要从collectionView:layout:referenceSizeForHeaderInSection 返回CGSize.zero,这样标题就不会被填充:

    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)
      }
    }
    

    那么无论你在哪里隐藏活动指示器(可能在异步操作的完成块中),你都应该调用collectionView.reloadData,所以viewForSupplementaryElementOfKind会被再次调用:

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

    【讨论】:

    • viewForSupplementaryElementOfKind 不接受 nil 的返回。
    • 哦,对不起,我完全错过了,但没关系。如果指示器正在动画,则需要在相应的委托方法中返回 CGSize.zero 以获取标题高度。保持viewForSupplementaryElementOfKind 不变,并使用相同的想法更新referenceSizeForHeaderInSection。
    猜你喜欢
    • 2020-03-02
    • 2023-04-04
    • 1970-01-01
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多