Brian Goodwin
Brian Goodwin

Reputation: 391

UICollectionView is not displaying - bug?

I am a bit of a novice with swift. I am trying to simply create a collection view that is placed within a specific region of the screen (landscape iPad iOS). It is possible that there is actually a collectionView bug... The following code produces the attached image. The problem is that the entirety of the collection view simply isn't displayed...

import UIKit

class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    var collectionView: UICollectionView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.sectionInset = UIEdgeInsets(top: 300, left: 500, bottom: 30, right: 10)
        layout.itemSize = CGSize(width: 32, height: 32)
        collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout)
        collectionView!.dataSource = self
        collectionView!.delegate = self
        collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
        collectionView!.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(collectionView!)
    }

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

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

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! UICollectionViewCell
        cell.backgroundColor = UIColor.orangeColor()
        return cell
    }
}

iOS simulator

Also, is there a way I can avoid using a FLOW LAYOUT? A rigid collection view is fine. I need a 10x10 collection view that won't vary in size or number of cells.

Upvotes: 1

Views: 745

Answers (1)

Daniel Storm
Daniel Storm

Reputation: 18908

For what you're trying to achieve using auto layout sounds like it would be your best option. You could alternatively set your UICollectionView's frame to be in the bottom right corner like so:

collectionView!.frame = CGRect(x: self.view.frame.size.width - collectionView!.frame.size.width, y: self.view.frame.size.height - collectionView!.frame.size.height, width: 400, height: 400)

But this is more of a temporary fix and would have to be considered for each device's, and future device's, layout.

Upvotes: 1

Related Questions