Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i set only one collection view for sizeForItemAt?

I have ViewController where have two collectionView but for one I want isPagingEnabled for cell and for anther collectionView 3 item for full frame width. How can I do that ?

MenuCollectionView for paging : it working perfectly

 func setupMenuCollection(){        
            if let flowLayout = menuCollectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
                flowLayout.scrollDirection = .horizontal
                flowLayout.minimumLineSpacing = 0
            }

            menuCollectionView?.backgroundColor = UIColor.white
            menuCollectionView?.contentInset = UIEdgeInsetsMake(50, 0, 0, 0)
            menuCollectionView?.scrollIndicatorInsets = UIEdgeInsetsMake(50, 0, 0, 0)

            menuCollectionView?.isPagingEnabled = true

        }

It is for manubarCollectionView : it is not working because here no else statement.

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

            return CGSize(width: view.frame.width / 3, height: manubarCollectionView.frame.height)
        }

    }
like image 747
cristan lika Avatar asked Dec 24 '22 22:12

cristan lika


2 Answers

It's simple, for second collectionView cell size either add else block and return CGSize or directly return CGSize after if block.

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

        return CGSize(width: view.frame.width / 3, height: manubarCollectionView.frame.height)
    }
    else {
        //return cell size for menuCollectionView
        return collectionView.frame.size
    }
}

OR

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

        return CGSize(width: view.frame.width / 3, height: manubarCollectionView.frame.height)
    }

    //return cell size for menuCollectionView
    return collectionView.frame.size
}

Note: You need to put this type of condition in all dataSource and delegate methods of collectionView to differentiate two collectionView.

like image 113
Nirav D Avatar answered Jan 07 '23 17:01

Nirav D


See Cristan ,

menuCollectionView?.contentInset = UIEdgeInsetsMake(50, 0, 0, 0)

above method is used for setting space between two cell.

And below method collectionViewLayout is used for setting cell Width and Height

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    if manubarCollectionView == collectionView {
        return CGSize(width: view.frame.width / 3, height: manubarCollectionView.frame.height)  
    }  
    else
    {  
        return CGSize(width: 100, height: 100)  
    }  
}

If you want to handle different cell for two collection view then check

how to use two CollectionView on same view controller

like image 28
Vivek Gajbe Avatar answered Jan 07 '23 18:01

Vivek Gajbe