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

时间:2015-07-27 19:59:18

标签: ios swift uicollectionview uicollectionviewcell

我有UICollectionView segmented control来切换数据。但是,如何以编程方式更改UICollectionViewCell尺寸属性 ?然后我可以为每种数据类型适当地定制每种样式。

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

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

enter image description here

9 个答案:

答案 0 :(得分:87)

确保符合您班级的UICollectionViewDelegateFlowLayout (对于swift4)

eg. class MyClass: UICollectionViewDelegateFlowLayout

在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

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

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

答案 1 :(得分:61)

Swift 3

如果您使用的是Swift 3,来自@ sumit-oberoi回答的方法不会被称为。为了使其正常工作,请进行此调整:

  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
    }
    
  3. 请注意更改:

    1. 在方法名称
    2. 中的_之前添加collectionView
    3. NSIndexPath更改为IndexPath

答案 2 :(得分:29)

Swift 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()
}

答案 3 :(得分:5)

如果您使用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)

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

myCollectionView.setCollectionViewLayout(layout, animated: false)

然后消息将消失

答案 4 :(得分:1)

尝试使用UICollectionViewDelegateFlowLayout方法。在Xcode 11或更高版本中,您需要将情节提要中的“估计大小”设置为“无”。

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

答案 5 :(得分:1)

  1. 在故事板中 选择 Collection View 并将 Estimate Size 属性设置为 NONE 在 Size Inspector 中,如下所示:

this

  1. 在视图控制器中为视图控制器添加 UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let itemsPerRow:CGFloat = 2
    let padding:CGFloat = 20
    let itemWidth = (collectionView.bounds.width / itemsPerRow) - padding:CGFloat
    let itemHeight = collectionView.bounds.height - (2 * padding:CGFloat)
    return CGSize(width: itemWidth, height: itemHeight)
}

答案 6 :(得分:0)

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

答案 7 :(得分:0)

Swift 5:

  1. 符合 UICollectionViewDelegateFlowLayout 协议,这意味着您需要将该协议放在类的继承中或作为扩展。像这样:

    class ViewController: UIViewController, UICollectionViewDelegate, 
    UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    }
    
  2. 实现 sizeForItemAt 方法:

    func collectionView(_ collectionView: UICollectionView, 布局 collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: 索引路径) -> CGSize { 返回CGSize(宽:100,高:100) }

您可以在 CGSize 构造函数中输入您想要的宽度和高度。

答案 8 :(得分:0)

     /// this method is used for flow layout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let numberOfItemsPerRow:CGFloat = 3
    let spacingBetweenCells:CGFloat = 2

    // 2 is the value of sectionInset value
    
    let totalSpacing = (2 * 2) + ((numberOfItemsPerRow - 1) * spacingBetweenCells) //Amount of total spacing in a row
    if let collection = self.collectionAccounts{
        let width = (collection.bounds.width - totalSpacing)/numberOfItemsPerRow
        
        return CGSize(width: width, height: width)
    }else{
        return CGSize(width: 0, height: 0)
    }
}