UICollectionView 中的最后一项未对齐 - Swift [英] Last item in UICollectionView not alligned - Swift

查看:22
本文介绍了UICollectionView 中的最后一项未对齐 - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到过一些与此类似的问题,但没有一个真正对我有帮助.我的收藏视图中的最后一个项目总是低于其他项目,如下图所示:

I have seen some questions similar to this one but none have really helped me. The last item in my collection view is always lower than the other items, as you can see in the image below:

如果我增加 UICollectionView 的高度,那么最后一个图像会正确对齐,但是 UICollectionView 的顶部和底部之间存在巨大的差距,如下图:

If I increase the height of the UICollectionView then the last image alligns correctly but there is a huge gap between at the top and bottom of the UICollectionView as seen in the image below:

单元格的大小由以下代码控制:

The sizing of the cells are controlled by this code:

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    let image =  UIImage(data: imageArray[indexPath.row])

    var size = image?.size
        let height = size!.height
        let width = size!.width
        let imagewidth = (width / height) * 214



    return CGSize(width: imagewidth, height: 214)
}

据我所知,这应该使每个图像的高度相同,这意味着原始图像的高度无关紧要.

From what I understand, this should be making the height of each of the images the same, meaning it doesn't matter what the height of the original image is.

我还有另一个集合视图,其中最后一个项目已正确对齐并以完全相同的方式设置,所以我有点困惑.你认为问题是什么?我已经读到这可能是一个错误,但是它周围有吗?我仍在学习 Swift 和一般编程,因此如果您有任何其他建议给我,我将不胜感激.

I also have another collection view in which the last item is aligned correctly and is set up in the exact same way so I'm a bit confused. What do you think the problem is? I have read that this could be a bug, but is there anyway around it? I am still learning about Swift and programming in general so if you have any other tips for me, they would be massively appreciated.

谢谢

推荐答案

如果 UICollectionView 的顶部和底部之间存在巨大的差距,可以通过设置 viewController's automaticallyAdjustsScrollViewInsets 假.

If there is a huge gap between at the top and bottom of the UICollectionView , you can solve the problem via setting viewController's automaticallyAdjustsScrollViewInsets false.

override func viewDidLoad() {
        super.viewDidLoad()

        //remove the blank of top and bottom in collectionView
        self.automaticallyAdjustsScrollViewInsets = false

    }

这篇关于UICollectionView 中的最后一项未对齐 - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆