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
Related
I am working on a wallpaper app with images of different sizes. I am calculating the sizes of cells dynamically with respect to image size. But how I can set the positions of cells dynamically with respect to its upper cell .. see the screenshots below:
How can I create a dynamic layout like this.. see screenshot:
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
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)
I have an ImageView inside of a self-sizing table cell that has a content mode of Aspect Fit.
When the table fully loads the image itself looks great, it resizes to fit within the width of the cell while still keeping the original ratio.
However, the height of the table cell is as large as if the Aspect Fit content mode was never applied to the image view.
The red in this image is the image view and the space above and below the actual image is what I'm trying to avoid.
When I replace Aspect Fit with Aspect Fill, this is what the same cell looks like:
This is what I mean when I say it looks like the image view is using the height of the image before Aspect Fit is applied.
Am I missing a constraint or setting in the ImageView?
Any help is appreciated!
If the images are being downloaded from a server, they will take a second or a millisecond to be downloaded. The time they take to download is noticeable to the user. The best user experience is explicitly setting the height and width before the image is downloaded. If you are using a third party API to fetch the image (i.e. facebook or youtube API), the return will include the image URL along with the height and width for the image. This way you can set the height and width of the cell and then wait for the image to download without any interruption or change in the size of cells. This would be the best way to do it. By having the height and width set explicitly, you don't need to worry as much about the content mode to use for the image view. I would only recommend using a self sizing UITableViewCell if you are explicitly setting the height and width of image view within that cell. I hope this helps!
I just saw your most recent comment. You can adjust the size of each image view within each cell depending on the image that is being returned. You don't need to set a fixed height for the image view. You can change the height of each cell depending on the image that is in that cell. I would just recommend doing this before the image is downloaded.
If you are using storyboard, then one way to do this is creating an outlet for the height constraint of the image view.
I am showing images downloaded from an URL in a table view whose custom cells have UIImageViews.
Now my requirement: I want to fix the height of that imageview and cell according to the height of that image in that cell.
The steps to perform are the following: (I won't write the code for you, just guide)
in heightForRow method you pick the image from the data source
then you learn its size (image.size)
return the value like
size.height+SOME_CONSTANT, where SOME_CONSTANT is the space for
other content