Resize UICollectionViewCell to fit image - ios

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

Related

Calculate proper line size in UICollectionview

Im using a library called JTAppleCalendar and it uses a CollectionView to draw out the Calendar. I want that my Cells have small lines separating them so i set the calendarView.minimumLineSpacing to 0,4 but what i get is a mismatch of lines (some are thicker than they should be) see in picture
My goal is to get equal lines. The width of the Calendar is the device width (in my example 375 (iPhone 12 mini)) and the height is fixed at 295.
I have researched a lot and, found out that this is some kind of UIKit problem because the cell sizes are not an even number. ( maybe this issue is also useful )
Please help me solve this problem.

Content of Grid list to fill the height

I am placing content in the grid list component and I want the grid tile to grow accordingly to the content it has. I tried to use rowHeight="fit" as documentation states but it doesn't seems to work.
Here an example
MatGridList overall size is directly related to the rowHeight setting. The default rowHeight value is a ratio 1:1 (which means column width equals row height).
When you use a ratio, the list will have a fixed width/height ratio overall, so if the list can't grow in one direction because of window or layout restrictions, it can't grow in any direction. This is why your example looks the way it does. If you expand the width of the window, you'll see the list expand vertically as well. You can set a different rowHeight value to make the tiles taller than wide such as rowHeight="1:2" (that seems backwards to me but that's how it works).
When you use rowHeight="fit", this doesn't fit the list to the content, it fits the row heights to the list height, but as noted in the documentation, you must set a height on the list or a parent for this to work properly.
Setting a fixed value for rowHeight does what it sounds like.

Mixing self sizing cells with calculated sizes

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.

Spacing In VLayout

I am working inside a Tabpane and I want to have 2 buttons on the bottom-right of the tap-pane, so I thought I just add a LayoutSpacer, but this resulted in
but I wanted it to look like
How can I make my Layoutspacer "bigger"?
In Smart GWT there are different methods to set the size of the component's vertical dimension.
Try with any one
canvas.setHeight100()
canvas.setHeight("100%")
canvas.setHeight("*")
Layouts may specially interpret percentage sizes on their children, and also allow "*" as a size.

Avoiding collection view "stretched" cells

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?

Resources