Setting and changing width/height of UICollectionView cells with UICollectionViewLayout - ios

I'm using drag and drop to drag images from google onto a collection view. The images are all supposed to have equal widths. After I scroll a bit, some of the cells look like they have equal widths, but some images are too big.
The collectionView has a flow layout and horizontal scroll direction.
In the following gif, the images start out as if they may have the same width(I can't really tell). When I drag an image in, they resize, and you can immediately see that the two on the left no longer have the same width, the one image is huge, and the huge image has a smaller duplicate(there should only be one beach seal picture), but the dragged in image is missing(until I scroll back and forth). When I close and reopen, the widths look ~correct again(but the images are place sporadically, I'd like a nice compact layout). When I scroll up, the images become the wrong sizes again. (My search was for the word 'seals' if you're interested in duplicating)
This is my code for sizing the cells
var gallery = [(URL,Float)]()
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = cellWidth
let (_,ratio) = gallery[indexPath.item]
let height = Float(width) * ratio
return CGSize(width: Int(width), height: Int(height))
}
Update
I was able to stop the images from getting to big by setting Estimated Size to None in my storyboard. Now it appears that not every image is as wide as it is supposed to be(each image should have the same width). Also there's a lot of space between my cells which I don't want. There doesn't seem to be a pattern to how much space there is either.
Github
My full project on Github

Related

How to get the "sizeForItemAt" for a UICollectionView, when loading in different sized images

I'm using this library, CHTCollectionViewWaterfallLayout
(https://github.com/chiahsien/CHTCollectionViewWaterfallLayout) to try to replicate a feed that's similar to the Pinterest layout or the Imgur layout shown in the screenshot below. There's 2 columns and then each card has a variable height based on the height of the image it's showing.
What I'm having trouble with, is calculating the right width and height for sizeForItemAt, since I'm loading these images from URLs:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
I believe I can store the width and height of each image, but then how do I calculate the height I'll have to add from the little section beneath the picture, where there is more text. Currently I've built these cards in SwiftUI which is great for dynamically fitting the text, but I don't know how to get the size. Do I need to build these cards in Swift, or is there a way for me to get the additional height I have to add to the images height, for the aspect ratio to come out correctly?

Autosizing UICollectionView cell embedded in UITableView not working

I have spent a few day's now trying out things suggested on various SO posts but have not been able to figure this out. I'm probably looking right at the issue!
I am making a switch over from Android to iOS and seem to be having some trouble with autolayout. Specifically, getting the UICollectionView to resize to the height of its content. Each collection view can have a different cell size based on its content but the cell size will be the same for each item on a given row.
The UICollectionView is nested inside a UITableViewCell. The UITableViewCells appear to be resizing to its content height correctly and looking at the Debug View Hierarrchy the issue seems to be the UICollectionView.
I have created a mini demo and put it on this github (link below). I have thrown in a bunch of test data so the bottom part of the ViewController VC is a bit messy. The demo just illistrates how each row can have different content size. There can be several table rows of the same style.
NOTE: I had to add the following line to CategoryRowCell.swift in order for me to get the below screen shots. Without this line the UICollectionView height becomes 0!
...
collectionView.heightAnchor.constraint(equalToConstant: 250)
...
GitHub link
So here is what it looks like right now..
scrolled down
Notice the gaps at the top and bottom of the horizontal lists. Here is what i am expecting..
Here is part of the debug view hierarchy. You will see that the collection view already has top and bottom margins (good), but the red lines show the excess height being applied to the collection view. The green line indicates the height i am expecting. I am wanted the collection view to have a height of its content.
I think it would be better to do it the other way around, having the UICollectionView as the parent and UITableView as the child. Since the first is more flexible and customizable.
Anyway for your case I think you need to take a look at two things:
1- To have a better control of the cell size you can use this func for UICollectionView:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
// The desired size based on the value at that row (the name for example)
if objects[indexPath.row].name == "some condition" {
return CGSize(width: 150, height: 50)
}
else {
return CGSize(width: UIScreen.main.bounds.width, height: 75)
}
}
2- The size inspector values for the UICollectionView and UITableView (you might have some extra spacing in one of them)

UICollectionViewCell resizing in an image array

Hi I have an image array which consists of images with different sizes. I am going to display them in a Horizontal UICollectionView. And I need to show these images with same height , same padding (not the cell interim space, but the look of the image) and different widths.
Example:
Any help would be highly appreciated!
You need to implement this method , inside it generate your random width and pass it to the width parameter
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width:<#variableWidth#>,height:<#sameHeight#>)
}
Well creating your custom collectionViewLayout is also an option. It will give you more flexibility but at the same time you have to do all the calculations yourself.
Looking at your case, it appears you can use a horizontal stack view embedded inside a scroll view. So in this approach you just need to add your views with different widths and set the stack view fill property to proportional. You can refer this link for more details.

Xcode image occupying whole tableView instead of cell

So I am creating a prototype which I will use later.
I have a table view with one cell, then I will make the whole table populate several cells (that is working)
My problem is, the image on my cell, instead of occupying the cell to occupies the whole table view as follows
Any ideas?
Thanks
Storyboard
Simulator
EDIT
repo: https://bitbucket.org/eduardoreecreate/coderswag-ios
Try setting the image view's content type to AspectFit and ClipToBounds to true
By default, an image view under the influence of auto layout wants to be the size of the image. Your image is big. Therefore the image view is big. Therefore the cell itself is big, because you are autosizing the cell height to match the height of its contents.
The simple solution is: don't do that! Before you put the image into the cell, munge it in code so that it is the correct height. It is very foolish to put a huge image into a small table cell in any case, or any small image view, because you're still wasting all that memory. Always try to crop / shrink the image before putting it into the interface.
More elaborately, you can give the image view an absolute height constraint to keep it from growing beyond a certain height. Or, as you've already been told, don't use automatic cell height; set the cell height to some absolute value by implementing heightForRow.
Implement heightForRowAt UITableView's delegate
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 150
// Customize or write a logic as per your requirement like for 1/4 of screen UIScreen.main.bounds.size.height / 4
}
If you select the cell in IB, then the Size Inspector shows Row Height at the top (with a "custom" checkbox), but if you select the whole table view the Size Inspector shows Row Height at the top there too (no "custom" in this case). The table view setting is used for dynamic cells. (Not sure if it's intentional)

UICollectionViewCell Width same but Hight different for images

I am working on UICollectionview, in which UICollectionViewCell I want to display images that are available in Webservice.
Now the thing is that these images have different hight and Width. I want to display images in UICollectionViewCell that automatically wrap its height according to image without leaving equal hight of cells.
This is very samilir to android view width :- match_parent and Height:- wrap_content.
Please refer images. Ideal Case and What i am getting.
Thanks in advance.
Image 1 Ideal Case
Image 2 What i am Getting

Resources