Search code examples
swiftxcodeuicollectionviewswift3xcode8

How do I change the size of a 2x2 UICollectionView and fit the whole screen: Swift 3 Xcode 8


Hi I am creating an app which requires a grid of images. As an example I am using a 2x2 grid of images in cells of a UICollectionView. I would like to know how I could decrease the annoying space in the middle and also make them have equal widths and heights to fit the whole screen. I know that I can do this with Interface builder. However, I want to do this programmatically because as the app progresses, there will be different levels and different number of squares - possibly a 10x10 also. I am new to UICollectionViews. I have referenced to many other questions, however most are in Objective C and there few that are in Swift don't seem to work with Swift 3.

Here is my full code:

import UIKit

class GameViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{

@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    collectionView.delegate = self
    collectionView.dataSource = self
let collectionViewLayout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout
    collectionViewLayout.minimumLineSpacing = 0
    collectionViewLayout.minimumInteritemSpacing = 0

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


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

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


    let image = cell.viewWithTag(1) as! UIImageView
    image.image = #imageLiteral(resourceName: "coffee")

    return cell;
}


func collectionView(_collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    return CGSize(width: self.collectionView.frame.size.width/2, height: self.collectionView.frame.size.height/2)
    //this method is not getting called 

}




}

I am also uploading an image of how it currently is and how I want it to be-

enter image description here

enter image description here

I hope you guys will help me- it will be very useful... Thanks!

I conformed to UICollectionViewDelegateFlowLayout and called sizeforitematindexpath but it did not work. On adding a breakpoint I realized that the method was not getting called. So is there anything wrong I'm doing here. Also Xcode gives me this warning - enter image description here


Solution

  • Make sure that:

    • Confirming to UICollectionViewDelegateFlowLayout.
    • collectionView's width = screen's width.
    • Min Spacing for cells is zero. You can change it from the Interface Builder (Select the collectionView -> show size inspector -> set min spacing to 0), or by implementing minimumInteritemSpacingForSectionAt returning zero.
    • If you want to fill the whole Screen height (I am asking because this is not what provided in your screenshot), collectionView's height = screen's height.

    Finally, implement sizeForItemAtIndexPath method:

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