【问题标题】:How to hide Refresh Control under UICollectionView如何在 UICollectionView 下隐藏刷新控件
【发布时间】:2018-02-14 18:09:25
【问题描述】:

我正在使用:

  • XCode 9
  • 斯威夫特 4

我的CollectionView 代码非常简单:

@IBOutlet weak var collectionView: UICollectionView!

override func viewDidLoad() {
   super.viewDidLoad()
   let refreshControl = UIRefreshControl()
   refreshControl.addTarget(self, action: #selector(onRefresh), for: .valueChanged)
   refreshControl.isUserInteractionEnabled = false

   collectionView.alwaysBounceVertical = true
   collectionView.refreshControl = refreshControl
}

@objc private func onRefresh(refreshControl: UIRefreshControl) {
   refreshControl.endRefreshing()
}

在我拉动collectionView 并出现refreshControl 后,如果我将collectionView 滚动回顶部,则refreshControl 将绘制在单元格上:

我找到了这个案例的解决方案:

refreshControl.layer.zPosition = -1

但现在Refresh Controll 位于单元格后面,并通过它们之间的空间查看:

我想要得到的结果非常简单且合乎逻辑。我假设它适用于TableView,因为其中的单元格具有背景填充。为什么 Apple 最初不通过 CollectionView 支持这种方法:

【问题讨论】:

  • 您应该确保在覆盖开始时调用 super.viewDidLoad() 来完成视图生命周期。
  • @SwiftRabbit 感谢您的回答,但我提供了代码的缩短版本。现在我根据你的提示编辑了答案
  • 您没有正确调用选择器。目前您正在使用#selector(onRefresh)。此处不传递任何参数。我想知道你的 onRefresh 是如何被调用的,或者你的代码是如何被编译的。
  • @Nitish 别担心,代码有效!
  • 请帮助我理解代码是如何工作的。因为据我了解,它甚至不会编译。

标签: ios swift uitableview uicollectionview uirefreshcontrol


【解决方案1】:

你只需要在设置了refreshcontrol的实例后添加以下内容:

        refreshControl.backgroundColor = UIColor.white
        refreshControl.tintColor = UIColor.black

希望对你有帮助~

【讨论】:

  • 太棒了!但是只指定背景颜色就足够了:refreshControl.backgroundColor = .white非常感谢!
猜你喜欢
  • 1970-01-01
  • 2021-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-06
相关资源
最近更新 更多