Setting and changing width/height of UICollectionView cells with UICollectionViewLayout

假装没事ソ 提交于 2019-12-23 23:24:16

问题


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

来源:https://stackoverflow.com/questions/58617380/setting-and-changing-width-height-of-uicollectionview-cells-with-uicollectionvie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!