1

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.

2
  • Your UIEdgeInsets are huge. Try something smaller. For example, layout.sectionInset = UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50) Commented May 23, 2015 at 21:04
  • Thanks for the response - maybe my understanding of UIEdgeInsets is wrong. Your recommendation helps, but lets say I want my collection within the bottom right corner - This is where I am getting the problem. If my screen dimensions are 1024x768, I would think that my edge insets should be something like (top: 500, left: 500, bottom: 0, right: 0)... no? Wouldn't this get me a collection view in the bottom right? Commented May 23, 2015 at 21:15

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.