Collectionview第二项从中心而不是顶部开始

时间:2020-05-10 16:39:11

标签: ios swift uicollectionview

我正在尝试为collectionview复制Instagram的搜索布局。我已经尝试过这样做,但是我的第二个项目从中间而不是顶部开始,这导致了问题。

实际输出:Actual Output

预期产量:Expected Output

这是我的代码

extension UIViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{



public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 12
}

public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CVCell", for: indexPath) as! CVCell
    cell.backgroundColor = .lightGray
    cell.lbl.text = "\(indexPath.item)"
    cell.lbl.textAlignment = .center
    cell.layer.borderColor = UIColor.black.cgColor
    cell.layer.borderWidth = 0.5

    return cell

}

public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if indexPath.item == 0{
        return CGSize(width: (UIScreen.main.bounds.width / 3) * 2, height: (UIScreen.main.bounds.width / 3) * 2)
    }
    return CGSize(width: UIScreen.main.bounds.width / 3, height: UIScreen.main.bounds.width / 3)
}

0 个答案:

没有答案