I have a UICollectionView where the width of the cells is dynamic based on text. When I set a spacing of 5px between all cells, this is the distance between the 2 longest cells but all the other ones are a lot more spaced.
I need the spacing to be always the same, even when the cells are very small or very big.
Any idea on how to achieve this?
So far I'm setting the minimumLineSpacingForSectionAt and the size of the cell is calculated based on the width of the text.
Related
I'd like to resize UICollectionViewCell to fit image that is sensibly resized and maintains dimensions/aspect ratio. If I simply set the size of cell to size of image it may be way too big. Also, If I run on different sized devices, the spacing isn't consistent. Must I implement collectionView programmatically to overcome this?
I see many apps that do this so its a very common problem that may already be solved as a framework.
Example:
UICollectionView is actually of arbitrary layout. It so happens that the default is UICollectionViewFlowLayout which is a grid, but you can change it to anything by implmenting your own instance of the UICollectionViewLayout protocol. Int the case you cited its pretty easy. The width of the cells is the collectionview (width minus the padding) divided by two. The height is the aspect ratio times the width + the spacing + the label height. all the cells with indices divisible by 2 go in the right column and the rest go in the left column. It should be easy to calculate these values for layoutAttributesForItem(at:). Unfortunately its a bit harder to calculate for collectionViewContentSize. Assuming your collectionview is sufficiently small I think its best to just precalculate all of the hieghts and use the cached values for these functions.
There're a lot of open sources on github You can refer to.
https://github.com/zhangsuya/SYStickHeaderWaterFall
My initial task is to have equal widths of cells and make them fill the entire row of the collection view with UICollectionViewFlowLayout.
Is there a way to calculate height of cells using autolayout, but provide width from sizeForItemAtIndexPath?
In sizeForItemAtIndexPath, calculate the height using systemLayoutSizeFittingSize: with a CGSize of your desired width, and a height large enough to fit your largest content.
e.g. for cell width of 100 (large maximum height of 999):
[collectionViewCell.contentView systemLayoutSizeFittingSize:CGSizeMake(100, 999)];
Beware though, if you have multiline UILabel's, as you will need to fiddle with preferredMaxLayoutWidth. More details, albeit for a table view, here.
I want to reduce the vertical spacing between two groups so that they nearly overlap each-other. is there any way to do this.
Yes - you can set the 'Spacing' to a value of your liking. It will effect the contents based on the layout type. Choosing a vertical layout will get you the vertical spacing you are after.
I'm setting up a collection view and customizing its cell's width, inset, and inter-item spacing. With the current size that means there would be two columns in portrait and three in landscape. I let flow layout do the rotation adjustments & calculations, but the result is a stretched middle column. I think what is happening is flow layout is doing the math for the cell padding/spacing and not getting clean whole numbers. Then it adjusts the size of the actual cell by half a point. The result is an aliased & blurry looking cell.
I'm had this issue multiple times and each time - I manually calculate the values for each orientation to come out as a clean whole number. While this works, it doesn't seem very efficient. I'm wondering if there is a better way to do this, and if FlowLayout has the ability to say, give that half point to a margin or the empty space between the cells?
Anybody know how big is the spacing between UITableViewCells in points ? That is I would like to know whether there are any screen spaces allocated between cells in an UITableView and how high are they, in each of these separator styles:
UITableViewCellSeparatorStyleNone – is it 0pt ?
UITableViewCellSeparatorStyleSingleLine – is it 1pt?
UITableViewCellSeparatorStyleSingleLineEtched – is it 1pt or 2pt?
The problem I'm currently facing is that I have a variable-height cells and I need to calculate how high a group of cells may be. Currently I'm using UITableViewCellSeparatorStyleSingleLine separator style and assuming that the inter-cell spacing is 1pt high but my calculations are often off.
That is, supposed that there are three cells, each 40 pt high, my assumption is that the total height will be 40 + 1 + 40 + 1 + 40 = 122 points high.
Questions are:
Is it true that a single line separator takes 1pt height between the cells?
If not, what is the height of the separator line, if any space is allocated at all (i.e. doesn't "eat" into the adjacent cell's space.
Thanks in advance.
This is a problem you can solve very easily by taking a screenshot from interface builder and then measuring it in Photoshop, however I've saved you the trouble:
The separator is not added to the the height of the table cell - it is drawn in front of the cell, so if your table cell height is 44 (the default) then the combined height of two cells will be 88, regardless of which separator style you choose.