ios 如何在 Swift 中以编程方式更改 UICollectionViewCell 大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31662155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 07:00:56  来源:igfitidea点击:

How to change UICollectionViewCell size programmatically in Swift?

iosswiftuicollectionviewuicollectionviewcell

提问by RileyDev

I have a UICollectionViewwith a segmented controlto switch between data. But how do I change the UICollectionViewCellsize properties programmatically? then I can custom each style appropriately for each data type.

我有一个UICollectionViewsegmented control数据之间切换。但是如何以编程方式更改UICollectionViewCell大小属性?然后我可以为每种数据类型适当地自定义每种样式。

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

例如,将宽度更改为 520 点,将高度更改为 100。我是 iOS 开发的新手,并且对 CSS 有经验:/

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)

我知道如何在 Xcode 中做到这一点(显然),但不知道如何快速更改它的正确方法。非常感谢任何想法或反馈(Y)

enter image description here

在此处输入图片说明

回答by Sumit Oberoi

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

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

eg. class MyClass: UICollectionViewDelegateFlowLayout

Prior to Swift 3

在 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
    }

// For swift 3

// 对于 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
}

// For swift 4

// 对于 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
        }
    }

use the same code as shown above for swift 3 but just make sure your class conforms to protocol UICollectionViewDelegateFlowLayout

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

回答by JPetric

Swift 3

斯威夫特 3

Methods from @sumit-oberoi answer won't be calledif you are using Swift 3. To make it work make this adjustments:

如果您使用的是 Swift 3,则不会调用来自 @sumit-oberoi 答案的方法。要使其工作,请进行以下调整:

  1. Conform to the UICollectionViewDelegateFlowLayoutprotocol.

  2. Implement these methods:

    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
    }
    
  1. 符合UICollectionViewDelegateFlowLayout协议。

  2. 实现这些方法:

    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
    }
    

Notice the changes:

注意变化:

  1. add _before collectionViewin the method name
  2. NSIndexPathchanges to IndexPath
  1. 在方法名称_之前添加collectionView
  2. NSIndexPath更改为 IndexPath

回答by Gaurav Rami

Swift 3

斯威夫特 3

override func viewDidLoad() {
    super.viewDidLoad()

    let cellSize = CGSize(width:80 , height:80)

    let layout = UICollectionViewFlowLayout()
    layout.scrollDirection = .vertical //.horizontal
    layout.itemSize = cellSize
    layout.sectionInset = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
    layout.minimumLineSpacing = 1.0
    layout.minimumInteritemSpacing = 1.0
    myCollectionView.setCollectionViewLayout(layout, animated: true)

    myCollectionView.reloadData()
}

回答by Heo ??t Hades

if you use Gaurav Rami's code bellow

如果您使用以下 Gaurav Rami 的代码

let cellSize = CGSize(width:80 , height:80)

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical //.horizontal
layout.itemSize = cellSize
layout.sectionInset = UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
layout.minimumLineSpacing = 1.0
layout.minimumInteritemSpacing = 1.0
myCollectionView.setCollectionViewLayout(layout, animated: true)

and get this message "Snapshotting a view that has not been rendered results in an empty snapshot. Ensure your view has been rendered at least once before snapshotting or snapshot after screen updates." you should remove animated when setting view layout for collection view

并收到此消息“对尚未呈现的视图进行快照会导致空快照。确保您的视图在快照之前或屏幕更新后的快照之前至少已呈现一次。”您应该在为集合视图设置视图布局时删除动画

myCollectionView.setCollectionViewLayout(layout, animated: false)

then the message will disappear

然后消息就会消失

回答by Muhammad Shariq Hasnain

Try to use UICollectionViewDelegateFlowLayout method. In Xcode 11 or later, you need to set Estimate Size to none from storyboard.

尝试使用 UICollectionViewDelegateFlowLayout 方法。在 Xcode 11 或更高版本中,您需要将 Storyboard 中的 Estimate Size 设置为 none。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: 
UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  let padding: CGFloat =  170
  let collectionViewSize = advertCollectionView.frame.size.width - padding
  return CGSize(width: collectionViewSize/2, height: collectionViewSize/2)
}

回答by Ananda Aiwale

first Conform protocol the UICollectionViewDelegateFlowLayout

set width and height  UICollectionViewCell 

UIcollectionViewCell Hight = 300;
UIcollectionViewCell Width = 380;

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout,sizeForItemAt indexPath: IndexPath) -> CGSize {
      return CGSize(width: CGFloat((380)), height: CGFloat(300))
    }