I used the following code at a UICollectionViewController
override func viewDidLoad() { self.collectionView!.alwaysBounceVertical = true let refresher = UIRefreshControl() refresher.addTarget(self, action: "refreshStream", forControlEvents: .ValueChanged) refreshControl = refresher collectionView!.addSubview(refreshControl!) } func refreshStream() { print("refresh") self.collectionView?.reloadData() refreshControl?.endRefreshing() }
Now I need it to work with a UICollectionView
inside a UIViewController
and I googled for an hour now but can't get it working. I appreciate any help.
Since iOS 10, the UITableView and UICollectionView classes have a refreshControl property. You can add a refresh control to a table or collection view by assigning an instance of the UIRefreshControl class to this property.
New swift code changed in calling action method you could do rewrite like this
@IBOutlet weak var collectionView: UICollectionView! var refresher:UIRefreshControl! override func viewDidLoad() { super.viewDidLoad() self.refresher = UIRefreshControl() self.collectionView!.alwaysBounceVertical = true self.refresher.tintColor = UIColor.red self.refresher.addTarget(self, action: #selector(loadData), for: .valueChanged) self.collectionView!.addSubview(refresher) } func loadData() { self.collectionView!.refreshControl.beginRefreshing() //code to execute during refresher . . . stopRefresher() //Call this to stop refresher } func stopRefresher() { self.collectionView!.refreshControl.endRefreshing() }
var refreshControl:UIRefreshControl! override func viewDidLoad() { super.viewDidLoad() self.refreshControl = UIRefreshControl() self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh") self.refreshControl.addTarget(self, action: #selector(PricingPlansCollectionViewController.reload), for: .valueChanged) collectionView!.addSubview(refreshControl) } func refresh(sender:AnyObject) { //DO }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With