简体   繁体   中英

How to set custom collectionView Cell Frame?

I want first UICollectionViewCell by full width of screen and further UICOllectionViewCell is 2*2.

For that i have tried below code.

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

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if indexPath.item == 0 {
        let cell : FirstCell = collectionView.dequeueReusableCell(withReuseIdentifier: "FirstCell", for: indexPath) as! FirstCell

        cell.layoutSubviews()
        return cell
    }
    else {
        let cell : SecondCell = collectionView.dequeueReusableCell(withReuseIdentifier: "SecondCell", for: indexPath) as! SecondCell

        cell.layoutSubviews()
        return cell
    }
}

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

    if indexPath.item == 0 {
        let width   = (UIScreen.main.bounds.size.width - 16)
        return CGSize(width: width, height: 200)
    }
    else {
        let width   = (UIScreen.main.bounds.size.width - 24) / 2
        let height  = (UIScreen.main.bounds.size.width * 200) / 320
        return CGSize(width: width, height: height)
    }
}

But the Output comes as below.

在此处输入图片说明

Requirement: First Green Cell is of full Width and 2nd Yellow cell comes not from Center but it comes from left side and 3rd, 4th and so on cell comes 2*2

Any help will be appreciated.

Thanks in Advance.

To achieve this, you should take 3 section in collection view.

  1. section 1 - 1 cell
  2. section 2 - 1 cell (width should be less than half of available width for collection cell)
  3. section 3 - number of cell you wanted

extension ViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 3
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return section == 2 ? 4 : 1
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        switch indexPath.section {
        case 0:
            return CGSize(width: UIScreen.main.bounds.width - 20, height: 200)
        case 1:
            return CGSize(width: (UIScreen.main.bounds.width - 30) / 2, height: 200)
        default:
            return CGSize(width: (UIScreen.main.bounds.width - 30) / 2, height: 200)
        }
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectioncell", for: indexPath)
        cell.backgroundColor = .red
        return cell
    }
}

Your view will look like:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM