I want to implement a pull to refresh feature on my current UIViewController. I have 10 buttons - 5 rows of vertical stack views in one horizontal stack view to create a grid. I would like to be able to pull to refresh here to run a function and update the button labels. What are my options?
-
can you show me your stackView code in your viewController? I just want to see how you hooked up the stackView – Swifty Nov 18 '18 at 23:52
3 Answers
You can't add a refresh control into a stack view for the purpose of refreshing the content.
The important thing here is you have to add the refresh control into a scroll view to let it be functional as expected:
UIRefreshControl
A standard control that can initiate the refreshing of a scroll view’s contents.
For this case I would encourage to let your grid to be as a UICollectionView
instead of stack view.
In addition, What if you tried to add another button into the grid? the container view should be scrollable, which means that a collection view would more maintainable.
Furthermore, you might be able to get the leverage of the UICollectionViewController. Checking the following could be useful:
Your best option is to go with a UICollectionView
. Create a custom cell to you desired needs. After that you can add a refresh controller:
// Global variable
let pullToRefreshData = UIRefreshControl()
// Where you initialise the collectionView (eg. viewDidAppear())
collectionView.refreshControl = pullToRefreshData
pullToRefreshData.addTarget(self, action: #selector(refreshData), for: .valueChanged)
// This will be called when you pull from top of the collectionView
@objc func refreshData() {
// Maybe you need to call a server API, stop the reloading animation when you have the data
pullToRefreshData.endRefreshing()
// Refresh the data from the collectionView
collectionView.reloadData()
}
If you only need to use your implementation, maybe you can provide a context.

- 30,560
- 17
- 97
- 143

- 477
- 6
- 18
For this use case I'd suggest using UICollectionView
, which has build in support for UIRefreshControl
. But if your buttons are somewhat different or you want a bit easier and less robust solution, you could simply embed your UIStackView
in UIScrollView
and set it's refreshControl
.
See: https://developer.apple.com/documentation/uikit/uiscrollview/2127691-refreshcontrol

- 71
- 1
- 8