Pull To Refresh Stack View - swift

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?

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.
UIRefreshControl
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:
Is completely static UICollectionView possible?
How to create a static UICollectionView.
How to use pull to refresh in Swift?

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.

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

Related

ScrollView.refreshControl = refreshControl does not show custom settings. Function executes, but no display feedback

I have a ScrollView with 3 collection views within it. I implemented a refresh control. The refresh control correctly functions in the sense that it triggers the required function - and even pulls down the scroll view with a delay.
However, setting any of the refreshControls properties (such as text/background colour) have 0 effect when I add the refresh control to my scrollView like so:
scrollView.refreshControl = refreshControl
When I use the old method:
scrollView.insertSubview(refreshControl, at: 0)
I am able to see the text/spinner, but it's buggy.
Is there a reason that the refreshControl's properties might be hidden from view when not using insertSubview?
Thanks to a user named Matt over on this question:
UIRefreshControl not showing in landscape when in a navigation controller with large titles
I was able to identify that this indeed was caused by Large Title Preferred: Automatic set in Storyboards for my navigation titles. Even though my nav bar is hidden on the view controller, the undesired effect of it hiding my refreshControl still existed. To fix this, as Matt says:
"If you turn off Prefers Large Titles everything is fine. I've played with some other settings, but no effect."

Creating tableview cell content programmatically

I want to create UItableViewCell ui programmatically based on API response.
I'm using a API that output a set of articles, but the content of these articles could be deferent, for example some articles may not have a description, or some of them may not have a image. I want to create Tableview cells programmatically based on these data.
I have tried setting constraints in viewDidLoad method of the cell but It does not work.
Do you have any suggestions on how to do this?
Depending on how different the articles are from each other, you could either choose to design multiple different kinds of cells for your table view and choose which cell to use for a given row based on the data you have, or you could design a single cell that uses a stack view to hide the labels that you don't have data for. When you set a view inside a stack view to isHidden = true then the stack view will resize its subviews as if the hidden view does not exist.

How to add Activity indicator to end of UICollectionView

Hi I have been trying to add an Activity Indicator, or a Loading spinner, to the bottom of my collection view. I have searched online on how to do so but there is not a clear way to do so.
I do not want to use a cell for the indicator itself because my cells a pretty big. (2 cells take up the whole screen.) So I would just like to have a small loading spinner at the bottom for when users scroll down.
Through the research that I have done I have seen that some people use this function below and a separate class to build the spinner.
private func collectionView(_ collectionView: UICollectionView, willDisplaySupplementaryView view: UICollectionReusableView, forElementKind elementKind: String, at indexPath: IndexPath) -> UICollectionReusableView{}
But I do not know how to use it. I already created a class called LoadingViewController() but in all honesty don't know if it is event he right type. Currenlty I have it set as a UICollectionReusableView
For a little more background, I already added the ability paginate data. So when I scroll down the collection view, new cells are loaded. Now the problem is I want to show a little spinner at the bottom when the data is loading because if not the app stutters and looks like it is broken.
Or is there a better way to show load items in a collection view that does not cause the scroll to freeze?
Please let me know if you have any questions regarding my situation I'll try to answer them to the best of my ability.

make view controller scrollable with collection view

I need to create a view controller like this or like user instagram's profile
not exactly same, I just want to add label and buttons and collection view,
but I want all of this in scroll viewController
I think I can't do that from storyboard, so how I can make a view controller scrollable programmatically?
and then I want to add the label and buttons inside the scroll , I can do that programmatically , but how I can add collection programmatically inside it?
One of the possible solutions is to make the whole container a "UICollectionView" -or "UITableView", depends on what exactly you want to build-, and then, create a custom cell for each area depending on what do you want to display in it.
It might sounds a little bit strange in the beginning, but you will notice that it is a great technique to handle scrolling in your scene, including some of extra nice features, such as:
auto scroll content resizing for "UIKeyboardWillShowNotification" and "UIKeyboardWillHideNotification" events.
the ease of showing and hiding sections from the UI (actually they are cells!).
UPDATED: For example:
You can make the first part (red rectangle) as a UICollectionReusableView and customize it by adding your images and button in it, second part (blue square) as a UICollectionViewCell and so on...
This is the general idea of what how you can do it.

Two UICollectionView inside UITableView not UITableViewCell

I found this post here : http://ashfurrow.com/blog/putting-a-uicollectionview-in-a-uitableviewcell/
But,unfortunately,this doesn't meet my requirement.It was here.
My flow is similar to the one at App Store.But,What I am gonna do was implementing pull to refresh and pull up to load more at top and button of UITableView.
I can include both two UICollectionView at the UITableView.But,I can't resize second UICollectionView as the data included in its datasource.
Please take a look
As you can see there was 5 blue block at second uicollection view.But,they are not showing properly.Its there anyway that i can fix my collection view data by resizing uicollectionview?
Because second collection view data will be dynamic as soon as I implement pull to load more below at uitableview cell.So,i disable the second collectionview vertical scrolling.But,I want to appear all the items at second collectionview.
Any Help?I can provide sample if needed.Thanks.