SameerVerma
SameerVerma

Reputation: 13

How to make UICollectionView auto layout

How to make UICollectionView auto layout such that the number of items appearing on the screen at some moment is decided according to total number of items. I actually want to make such layout that if i have 9 items than all nine items should be displayed at the same time on the collection view. if items are 8 or something else then the size of the items should increase in such a manner that they still consume complete area of the collection view.

the condition i have plotted is as under:

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    var cell = collectionView.cellForItemAtIndexPath(indexPath) as CollectionViewCell

    if((cell.selected) && (indexPath.row%2 == 0))
    {
        cell.backgroundColor = UIColor.whiteColor()
        imageview.image = UIImage( named: a[indexPath.row])
    }
    else if((cell.selected) && (indexPath.row%2 != 0))
    {
        cell.backgroundColor = UIColor.whiteColor()
        imageview.image = UIImage( named: a[indexPath.row])
    }
}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
    var abc : CGSize!
    if(a.count == 1){
        abc = CGSize(width: self.view.frame.width, height: self.view.frame.height/2)
    }
    else if(a.count == 2) {
            abc = CGSize(width: self.view.frame.width/2, height: self.view.frame.height/2)
    }
    else if (a.count > 2) && (a.count<6){
        abc = CGSize(width: self.view.frame.width/3, height: self.view.frame.height/2)
    }
    else {
         abc = CGSize(width: self.view.frame.width/3, height: self.view.frame.height/5)
    }
         return abc
   }

func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool
{
    return true
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            return a.count
}

func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
    return true
}

Upvotes: 0

Views: 1303

Answers (1)

theprole
theprole

Reputation: 2354

From your question I understand that you want to very the UICollectionView's cell sizes so they cover the whole screen.

There are various ways you could achieve what you want, but the key will be to work with the UICollectionViewLayout.

For example, the UICollectionViewFlowLayout has an itemSize property.

Based on your content (for the cells), you could try calculating how much space they will occupy and then set the itemSize property of the FlowLayout. If all the cells are supposed to be the same size, this should be good enough.

You could also use the delegate method collectionView:layout:sizeForItemAtIndexPath: to set the size of each cell.

If that's not sufficient, because you want to change the arrangement of the cells as well for example, you will need to subclass the FlowLayout in order to calculate the size for each item. Have a look at this: https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/UsingtheFlowLayout/UsingtheFlowLayout.html

Ultimately, you need to start by calculating how many cells you'll need based on your content first.

Upvotes: 1

Related Questions