如何在 Swift 中以编程方式更改 UICollectionViewCell 大小? [英] How to change UICollectionViewCell size programmatically in Swift?

查看:26
本文介绍了如何在 Swift 中以编程方式更改 UICollectionViewCell 大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UICollectionView 和一个 segmented control 来在数据之间切换.但是我如何更改 UICollectionViewCell 大小属性 programmatically ?然后我可以为每种数据类型适当地自定义每种样式.

I have a UICollectionView with a segmented control to switch between data. But how do I change the UICollectionViewCell size properties programmatically ? then I can custom each style appropriately for each data type.

例如将宽度更改为 520 磅,将高度更改为 100.我是 iOS 开发新手,并且对 CSS 很熟悉:/

For example change the width to 520 points and height to 100. I am new to iOS development and am experienced with CSS :/

我知道如何在 Xcode 中执行此操作(显然),但不知道在 swift 中更改它的正确方法.非常感谢任何想法或反馈(Y)

I know how to do it in Xcode (obviously) but do not know the correct methods to change it in swift. Any ideas or feedback is much appreciated (Y)

推荐答案

确保你的类符合 UICollectionViewDelegateFlowLayout(对于 swift4)

Make sure you conform to UICollectionViewDelegateFlowLayout for your class (for swift4)

eg. class MyClass: UICollectionViewDelegateFlowLayout

在 Swift 3 之前

Prior to Swift 3

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

    }
//Use for interspacing
    func collectionView(collectionView: UICollectionView,
        layout collectionViewLayout: UICollectionViewLayout,
        minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat {
            return 1.0
    }

    func collectionView(collectionView: UICollectionView, layout
        collectionViewLayout: UICollectionViewLayout,
        minimumLineSpacingForSectionAtIndex section: Int) -> CGFloat {
            return 1.0
    }

//对于 swift 3

// For swift 3

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    sizeForItemAt indexPath: IndexPath) -> CGSize {

}

func collectionView(_ collectionView: UICollectionView,
                    layout collectionViewLayout: UICollectionViewLayout,
                    minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 1.0
}

func collectionView(_ collectionView: UICollectionView, layout
    collectionViewLayout: UICollectionViewLayout,
                    minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 1.0
}

//对于 swift 4

// For swift 4

extension MyClass: UICollectionViewDelegateFlowLayout { 
        func collectionView(_ collectionView: UICollectionView,
                            layout collectionViewLayout: UICollectionViewLayout,
                            sizeForItemAt indexPath: IndexPath) -> CGSize {

        }

        func collectionView(_ collectionView: UICollectionView,
                            layout collectionViewLayout: UICollectionViewLayout,
                            minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
            return 1.0
        }

        func collectionView(_ collectionView: UICollectionView, layout
            collectionViewLayout: UICollectionViewLayout,
                            minimumLineSpacingForSectionAt section: Int) -> CGFloat {
            return 1.0
        }
    }

对 swift 3 使用与上面所示相同的代码,但只需 确保你的类符合协议 UICollectionViewDelegateFlowLayout

这篇关于如何在 Swift 中以编程方式更改 UICollectionViewCell 大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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