Label on collection view header should be positioned above a specific cell - ios

I want to have a label above a certain position in my collection view. Thereby the label can have a different size. For this I thought I use the supplementary views - namely the header. An example of this behavior can be seen in the iOS calendar:
The month name is either written in short form it the available space is scarce (last column) or written with the full name if you have more space. Also the month name starts with the first of a month.
What I can think of is to create a supplementary view, pass the content view size and calculates the distance from the side by multiplying the number of cells with the cell width. The cell width is calculated through the set collection view size and the data in the header defines the number of the cell. But what about orientation change and things like that? You can't use a cells header because the label can go over multiple cells.
How would you implement such a header?
Solution:
I used container each having the same width. On this container I added my labels. Now I can configure to either center the labels or position them at the beginning of the container. It now works for all orientations! Thanks rdelmar for leading me to this idea!

Related

How do I change the horizontal spacing between cells in a horizontal scroll collection view

I have a collection view which is basically a horizontal table view. It's one line of cells that scrolls left and right. Sort of like a carousel without the fancy animation. I would like to be able to control the distance between cells. There is a property for doing that in the interface builde. It is in the collection view flow layout, in the inspector (in the tab with the little ruler where you usually see constraints). It is called Min. Spacing and has two variants: For Cells and For Lines. I changed the value of For Cells but it doesn't affect the spacing. Neither does changing the parameter minimumInteritemSpacing or using the collection view callback for specifying that parameter on a per section basis. Help!
The answer is that in a horizontal scroll collection view, the distance between cells, even the horizontal distance, seems to be controlled by the For Lines parameter (in IB) or minimumLineSpacing parameter of the CollectionViewFlowLayout.
This answer is also buried in some answers I saw more general question about cell spacing in collection views:
https://stackoverflow.com/a/48791318/826946 and
https://stackoverflow.com/a/37420529/826946
I decided to separate it out into a separate question that directly addresses the issue of horitzontal scroll collection views.

iOS - Dynamic height, data driven, vertically scrollable layout

I am trying to create the layout in the image
Here is the main requirements:
The content in each section is data driven and retrieved from the api.
The height of each section is dynamic, based on how many fields we have.
The scene height is dynamic.
The scene is vertically scrollable.
Here is what I tried to do:
- Using static cell UITableView
- Using stack view with a scroll view
In both cases I got stuck with dealing with stack views for the dynamic data fields, and I struggled in handling the dynamic height for each section.
Any Ideas?
Your requirement is pretty straight forward and can be met using a UITableView. Use dynamic UITableViewCell. The reason i choose dynamic table view cell is that you can resize the cell height according to content. Measure your content size inside the tableView:heightForRowAtIndexPath:.
The flow will be following:
Initiate the api call
While the data isn't available return the cell height according to your placeholder view (i.e placeholder image height)
While the data is available, process your data and reload the table view
After reloading the table view, table view will ask each cell's height once again, so this is the chance you get to calculate the content height. (I assume that you can measure the string height while the string width and font is given. Plz google that, thats obvious.)
return that calculated height.
Optimization Possibilities:
Instead of recalculating the cell height each time, you can calculate the cell height once and store that height inside a simple dictionary keyed with IndexPath.

How to keep auto-sized table cells format consistent after edition/deletion of rows using UIKit?

I have a simple table view with a unique label for the cell prototype. I have been following the recommendations in order to have proper auto-sizing cells:
setting auto-layout constraints for the label regarding its leading, trailing, top and bottom spacing in its content view,
setting the rowHeight property to UITableViewAutomaticDimension,
providing the estimatedRowHeight property (set to 44),
setting the number of lines for the label to 0.
I also have enabled the default Edit Mode which allows the reordering and deletion of rows (exactly like in the Alarm tab of the Apple Clock app).
Now, on to observed problem:
I provide two labels, one with a short text and a second with a longer text, so that the second row has twice the height of the first row.
I enter Edit Mode, and swap the position of the rows. So far, everything keeps its composure and the sizing is still correct, the longer label being in the top row.
When I swap the positon again to return to the original layout (short label on top and long label on bottom), the longer text is not spread over two lines in its row as it should, but turns into a one-line display with an ellipsis at the end. Moreover, the height of the cell remains the same and does not fit the now single-lined text.
And after exitting Edit Mode (compare with the first image):
I suspect that I might be missing some kind of reload operation after the moving or deletion of rows, but I did not find the relevant information. Similar sizing problems appear when deleting rows that have been previously reordered.
How to keep the cell sizing as well as the text formatting consistent after moving/deletion of rows?
Thank you very much.

Align two labels in a table cell?

I am having pretty crazy amounts of issues trying to align two labels inside a table cell:
The problem is I need both labels (the region is clickable) to be centred within their cell. I can only seem to get the layout centred based one or the other.
Is this too much to expect from autolayout?
Embed the labels in a new view, then centre that view with autolayout. Also, let this new view resize itself according to the labels
So:
- Add leading and trailing constraints between labels and their superview (new view)
- Centre the new view in the UITableViewCell
I just got home, so I opened up a fresh project, with good news :)
So I set up what I described earlier, 2 labels inside an UIView
Select the labels and go to the Size Inspector, there check 'Explicit' and in the 'Preferred Width' add the Maximum - make it the maximum it is allowed to be
Now in code, you can set the labels and they auto-resize!

iOS - Can I use xib file for variable height table cell where dynamic content and fixed content are mixed

I'm creating a twitter-like app and I'm using a Table view for the feed. Each table cell is a custom cell. I'm designing those cells using xib file. The cell contains profile picture, comment count and a dynamic message block, etc. There are discussions around how to build variable height table cell:
How can I do variable height table cells on the iPhone properly?
The question is, the dynamic content is in the middle of the cell. Below it is fixed height content. As far as I understand, xib file gives each element fixed X and Y value. Can I stack them together like what "div" would do in?
In IB, in the file inspector (first tab of the left panel), disable "Use autolayout" (if you use a recent version of Xcode)
Then select the label which will have a different height (the proper twitter message) in the size inspector, in the size inspector, set the autosizing properly.
The 4 "things" around describe if the label will keep these dimensions fixed or no (in your case, select the 4) and the 2 arrows in the middle describe if the label will autoresize when its parentView is resized.
Then has rdelmar said, you need to implement the tableView:heightForRowAtIndexPath: method of the tableView delegate
The proper way to do this is by using the table view delegate method tableView:heightForRowAtIndexPath:. How you do it depends on what kind of content you're adding, but somehow, you need to calculate the size of the cell needed based on the data for each row, and return that number. The cell has to be designed in such a way, and with the proper layout constraints, so that when it expands, the part or UI element that you want to expand (like a text view or a multi-line label) expands properly.

Resources