Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Kelley Muro
Kelley Muro
454 Points

New verson of CGRectMake Swift 4

I'm trying to make the cells in my collection view expand the entire width of the screen. I tried:

CGSizeMake(view.frame.width, 50)

Im getting an error trying to do this and it saying CGSizeMake isnt available in swift. what should I use instead.

Also below is the code of the functions for the cells

verride func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return 3
}

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

    return collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath)

}

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

// HERE IS WHERE ITS BROKEN have tried multiple things and none of it works
return CGSize(width: 50, height: 50)

}

}

class FeedCell: UICollectionViewCell { override init(frame: CGRect) { super.init(frame: frame) setupViews()

}

required init?(coder aDecoder: NSCoder) {

    fatalError("init(coder:) has not been implemented")

}

func setupViews() {

    backgroundColor = UIColor.white

}

}

2 Answers

Kelley Muro
Kelley Muro
454 Points

I ended up doing:

if indexPath.section == 1 { return CGSize(width: (view.frame.width / 3) -16, height: 150

}

and that worked without having to create an extension. Thanks for the advice tho. Got me on a good track.

Everton Carneiro
Everton Carneiro
15,994 Points

Make an extension of your collectionViewController conforming to UICollectionViewDelegateFlowLayout. Then just return the CGSize with the frame's width and height.

extension YourCollectionViewController : UICollectionViewDelegateFlowLayout {

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width, height: view.frame.height)
    }

}
Kelley Muro
Kelley Muro
454 Points

I created an extension and it builds but they are still square. for width i put (width: view.frame.width, 50)

Everton Carneiro
Everton Carneiro
15,994 Points

Do you have a collection view inside a view controller? If so, remember to set your collectionView as a delegate of this view controller.