Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide Refresh Control under UICollectionView

I'm using:

  • XCode 9
  • Swift 4

My CollectionView code is pretty simple:

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

After I pull collectionView and the refreshControl will appear, if I scroll collectionView back to the top, then the refreshControl is drawn over the cells:

enter image description here

I found the solution for this case:

refreshControl.layer.zPosition = -1

But now Refresh Controll lies behind cells and viewed through spaces between them: enter image description here

The result I want to get is very simple and logical. I assume that it works with the TableView since the cells in it have background filling. Why did not Apple initially support this approach with a CollectionView:

enter image description here

like image 395
Yury Avatar asked Sep 17 '25 10:09

Yury


1 Answers

you just need to add the following after the instance of refreshcontrol is set:

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

Hope this helps~

like image 195
Richter Avatar answered Sep 20 '25 02:09

Richter