【问题标题】:Move background view down with pull-to-refresh - swift使用下拉刷新向下移动背景视图 - swift
【发布时间】:2018-08-25 17:34:37
【问题描述】:

我有一个带有 UIRefreshControl 的 UICollectionView 以允许拉动刷新。当没有结果时,我为我的 collectionview 设置了一个自定义背景视图,如下所示:

collectionView?.backgroundView = someView(如here 所述)

但是,当我拉动刷新时,backgroundView 并没有随着 collectionView 向下移动。

如何在我的 collectionView 中拥有一个自定义的全尺寸视图,该视图通过 refreshControl 提供的下拉刷新功能下拉?

【问题讨论】:

  • 您可以使用其他第三方来显示 pull-to-refresh 吗?我使用相同的 backgroundView 概念,我使用这个 github.com/icodesign/ICSPullToRefresh.Swift
  • 您是使用collectionView 作为控制器视图的子视图还是直接使用UICollectionViewController
  • 我使用UICollectionView 作为UIViewController 的子视图

标签: ios swift uiview uicollectionview


【解决方案1】:

在我看来,与其将someView 分配给collectionView?.backgroundView,不如让someView 成为一个单元格。当您的 collectionView 数据源为空时,将此单元格显示为仅在 collectionView 上的全尺寸单元格。

import UIKit

class EmptyCell: UICollectionViewCell {
  override init(frame: CGRect) {
    super.init(frame: frame)

    let backgroundImage = UIImageView.init(frame: frame)
    backgroundImage.image = UIImage.init(named: "bg")
    backgroundImage.autoresizingMask = [.flexibleHeight, .flexibleWidth]
    self.contentView.addSubview(backgroundImage)
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

  @IBOutlet weak var collectionView: UICollectionView!

  var shouldShowEmptyCell = false

  let refreshControl: UIRefreshControl = {
    let refreshControl: UIRefreshControl = UIRefreshControl.init()
    refreshControl.addTarget(self, action: #selector(refreshCollectionView(_:)), for: .valueChanged)
    return refreshControl;
  }()

  override func viewDidLoad() {
    super.viewDidLoad()

    collectionView.register(EmptyCell.self, forCellWithReuseIdentifier: "EmptyCell")
    collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "NormalCell")

    addRefreshControl()
  }

  func addRefreshControl() -> Void {
    if #available(iOS 10.0, *) {
      collectionView.refreshControl = self.refreshControl
    } else {
      collectionView.addSubview(self.refreshControl)
    }
  }

  @objc private func refreshCollectionView(_ sender: Any) {
    DispatchQueue.main.asyncAfter(deadline: .now() + 1.5) {
      self.shouldShowEmptyCell = !self.shouldShowEmptyCell
      self.collectionView.reloadData()
      self.refreshControl.endRefreshing()
    }
  }

  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return shouldShowEmptyCell ? 1 : 20;
  }

  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if shouldShowEmptyCell {
      return collectionView.dequeueReusableCell(withReuseIdentifier: "EmptyCell", for: indexPath)
    }

    let cell: UICollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "NormalCell", for: indexPath)
    if indexPath.row % 3 == 0 {
      cell.backgroundColor = .red
    } else if indexPath.row % 3 == 1 {
      cell.backgroundColor = .blue
    } else if indexPath.row % 3 == 2 {
      cell.backgroundColor = .green
    }
    cell.layer.cornerRadius = 10
    return cell
  }

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 8;
  }

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 8;
  }

  func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if shouldShowEmptyCell {
      return collectionView.bounds.size
    }

    return CGSize.init(width: collectionView.bounds.width / 2 - 4, height: collectionView.bounds.width / 2 - 4)
  }
}

更多细节,你可以看看我的示例项目 https://github.com/trungducc/stackoverflow/tree/empty-collection-view-pull-to-refresh

【讨论】:

    猜你喜欢
    • 2018-12-14
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多