Pull to refresh in UICollectionView in ViewController - ios

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.

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)
}
#objc func loadData() {
self.collectionView!.refreshControl.beginRefreshing()
//code to execute during refresher
.
.
.
stopRefresher() //Call this to stop refresher
}
func stopRefresher() {
self.collectionView!.refreshControl.endRefreshing()
}

Swift 5 solution
As #fishspy already mentioned, that's the way to put your collection view inside a view controller, but I'm gonna share also how to connect your refresh control to the collection view in a cleaner way.
Since iOS 10 there's a dedicated property for the refresh control. Apart of that, I'd also recommend to directly initialise your refresh control as a property, declaring it private and doing the following things:
#IBOutlet private weak var collectionView: UICollectionView!
private let refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
refreshControl.addTarget(self, action: #selector(didPullToRefresh(_:)), for: .valueChanged)
collectionView.alwaysBounceVertical = true
collectionView.refreshControl = refreshControl // iOS 10+
}
#objc
private func didPullToRefresh(_ sender: Any) {
// Do you your api calls in here, and then asynchronously remember to stop the
// refreshing when you've got a result (either positive or negative)
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
}

From the storyboard, you need to link the collection view to the appropriate controller file, using Ctrl + Drag from the collection view object. It needs to be linked via an #IBOutlet.
Also, you should Ctrl + Drag from the collection view to the view controller object on the storyboard and select Data Source and Delegate so that the collection view is correctly linked.
Let me know if this helps or if I have misunderstood your situation.

For me, i had:
func setupCollectionView(){
self.refreshControl = UIRefreshControl()
self.refreshControl.attributedTitle = NSAttributedString(string: "Refreshing content...")
self.refreshControl.addTarget(self, action: #selector(self.reload), for: .valueChanged)
collectionView!.addSubview(refreshControl)
}
It still wasn't working (I believe i had constraints preventing it somehow), so i added:
collectionView.alwaysBounceVertical = true
then all was good. Just in case someone else is facing the same issue.
For a better understanding, the full implementation
#IBOutlet private weak var collectionView: UICollectionView!
let refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView()
}
func setupCollectionView(){
collectionView.delegate = self
collectionView.dataSource = self
collectionView.alwaysBounceVertical = true
self.refreshControl = UIRefreshControl()
self.refreshControl.attributedTitle = NSAttributedString(string: "Refreshing content...")
self.refreshControl.addTarget(self, action: #selector(self.refresh), for: .valueChanged)
collectionView!.addSubview(refreshControl)
}
#objc func refresh(_ sender:AnyObject) {
//do refreshing stuff
}

private let refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(youFunction), for: .valueChanged)
refreshControl.attributedTitle = NSAttributedString(string: "Fetching Data ...", attributes: nil)
DispatchQueue.main.async {
self.refreshControl.endRefreshing()
yourCollectionViewOrTableView.reloadData()
}

Related

Why does pull to refresh is not working on scrollview

While pulling down to refresh, the controller target method is not being called.
Why is this issue happening?
override func viewDidLoad() {
super.viewDidLoad()
scrollview.alwaysBounceVertical = true
scrollview.bounces = true
refreshControl = UIRefreshControl()
refreshControl.addTarget(self, action: #selector(didPullToRefresh), for: .valueChanged)
self.scrollview.addSubview(refreshControl)
}
#objc func didPullToRefresh() {
print("Refersh")
// For End refrshing
refreshControl.endRefreshing()
}
iOS 10 > UIScrollView has a refreshControl property. This refreshControl will appear when you create a UIRefereshControl and assign it to this property. No need to add UIRefereshControl as a subview for scroll.
func configureRefreshControl () {
// Add the refresh control to your UIScrollView object.
myScrollingView.refreshControl = UIRefreshControl()
myScrollingView.refreshControl?.addTarget(self, action:
#selector(handleRefreshControl),
for: .valueChanged)
}
#objc func handleRefreshControl() {
// Update your content…
// Dismiss the refresh control.
DispatchQueue.main.async {
self.myScrollingView.refreshControl?.endRefreshing()
}
}

Refreshcontrol in scrollview is not working in swift

I have a scrollable content in my view. I want to add pull to refresh in it. I try to add but action is not triggered.
I tried many couple of ways & re-search also but none of it worked for me. I have iOS 10 as minimum deployment target.
Try1:
let refreshControl = UIRefreshControl()
self.scl_view.alwaysBounceVertical = true
refreshControl.addTarget(self, action: #selector(pullToRefresh(_:)), for: .valueChanged)
scl_view.addSubview(refreshControl)
Try2:
let refreshControl = UIRefreshControl()
self.scl_view.alwaysBounceVertical = true
refreshControl.addTarget(self, action: #selector(pullToRefresh(_:)), for: .valueChanged)
scl_view.refreshControl = refreshControl
//MARK:- Refresh control
#objc func pullToRefresh(_ refreshControl: UIRefreshControl) {
// Update your conntent here
self.setupData()
//refreshControl.endRefreshing()
}
select your tablviewcontroller
goto inspector
find refreshing select enabled
goto document outline and select refresh control
control drag or right click(hold) drag to your controller file and call function on it to refresh data
it is not available on tableview
var refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
// Add Refresh Control to collection View
if #available(iOS 10.0, *) {
collectionView.refreshControl = refreshControl
} else {
collectionView.addSubview(refreshControl)
}
refreshControl.tintColor = UIColor(red:0.25, green:0.72, blue:0.85, alpha:1.0)
// Configure Refresh Control
refreshControl.addTarget(self, action: #selector(fetchData(_:)), for: .valueChanged)
}
#objc private func fetchData(_ sender: Any) {
DispatchQueue.main.async {
//Fuction which you want to call
self.refreshControl.endRefreshing()
}
}

How to add Pull to refresh controller to scrollview?

Hi in my application I want to add Pull to refresh controller to scrollview.
I write the following code but it don't getting any response.
When ever I scroll the view on that time add target method is not called.
#IBOutlet var scrool: UIScrollView!
var refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
// Add refresh control for the Home page scroll view.
self.refreshControl.tintColor = UIColor.appcolor
self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
self.refreshControl.addTarget(self, action: #selector(refresh(sender:)), for: UIControl.Event.valueChanged)
self.scrool.isScrollEnabled = true
self.scrool.alwaysBounceVertical = true
scrool.addSubview(refreshControl)
}
#objc func refresh(sender:AnyObject) {
// Code to refresh table view
self.getHomePageApi()
refreshControl.endRefreshing()
}
Hi I resolved this issue when I write this code in viewwillappear() except viewdidload() function it will working fine.
var refreshControl:RefreshControl!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
refreshControl = RefreshControl()
refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
scrool.refreshControl = refreshControl
}
#objc func refresh()
{
// Code to refresh table view
refreshControl.endRefreshing()
self.getHomePageApi()
}

UIRefreshControl appears on top of collection view items instead of behind

I have a UICollectionView with the regular pull to refresh implementation, but somehow the spinner and the "pull to refresh" text appear above the collection view items;
How can I make it behind the items?
This is how I add the UIRefreshControll to the UICollectionView
let refreshControl = UIRefreshControl()
refreshControl.attributedTitle = NSAttributedString(string: "Pull down to refresh")
refreshControl.addTarget(self, action: #selector(pullToRefresh), for: UIControlEvents.valueChanged)
collectionView?.refreshControl = refreshControl
The way I figure this out is to change the refreshControll zPosition to be behind every view with the following:
refreshControl.layer.zPosition = -1
Hope this helps anyone further.
Try this :
#IBOutlet weak var collectionView: UICollectionView!
var refresher:UIRefreshControl!
override func viewDidLoad() {
super.viewDidLoad()
let 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() {
//code to execute during refresher
.
.
.
stopRefresher() //Call this to stop refresher
}
func stopRefresher() {
self.refresher.endRefreshing()
}

Pull to refresh not performing refreshing function?

I am coding an app and have a set up queryAndAppend functions that query a DB and append the info pulled to a set of arrays (which are then used to iterate through cells in a table view). The query functions work appropriately as they are called in the viewDidLoad(). I want a pull to refresh function to additionally call the functions (and thus update the table cells) but it is not working properly. Here is the relevant code:
var refreshControl = UIRefreshControl()
override func viewDidLoad() {
super.viewDidLoad()
myQueryandAppend(completion: {
self.tableView.reloadData()
})
upComingQueryandAppend(completion: {
self.tableView.reloadData()
})
refreshControl = UIRefreshControl()
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
refreshControl.addTarget(self, action: #selector(MainPageVC.refresh(_:)), for: UIControlEvents.valueChanged)
}
func refresh(_ sender: AnyObject){
myQueryandAppend(completion: {
self.tableView.reloadData()
self.refreshControl.endRefreshing()
})
upComingQueryandAppend(completion:{
self.tableView.reloadData()
self.refreshControl.endRefreshing()
})
}
Not entirely sure why its not working as I have a very similar set up in a different view controller (with a different query/append function) that works properly with pull to refresh.
If the class is a UIViewController but not a UITableViewController, you need to add the refreshControl as a subview to the tableView manually:
tableView.addSubview(refreshControl)
So, viewDidLoad() should looks like this:
override func viewDidLoad() {
super.viewDidLoad()
myQueryandAppend(completion: {
self.tableView.reloadData()
})
upComingQueryandAppend(completion: {
self.tableView.reloadData()
})
refreshControl = UIRefreshControl()
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
refreshControl.addTarget(self, action: #selector(MainPageVC.refresh(_:)), for: UIControlEvents.valueChanged)
// here is the extra job!
tableView.addSubview(refreshControl)
}
In iOS 10+ you can just assign the refresh control directly to a tableview / scrollview's .RefreshControl property
tableView.RefreshControl = refreshControl

Resources