3

I have a simple collectionview controller like this:

class ViewController1: UICollectionViewController{

    override func viewDidLoad() {

        super.viewDidLoad()

        self.collectionView?.delegate = self
        self.collectionView?.dataSource = self

    }

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

        return 5
    }

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

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "P", for: indexPath)

        return cell
    }
}

In the cell there is only an uiimageview and it has this constraints:

enter image description here

My problem is this:

when I execute this code on iPhone 6 simulator I get this (right way): enter image description here

but when I execute this code on iPhone 5 simulator I get this (wrong way: the imageview starts before the screen on the left and the width and height are wrong respect to the constraints): enter image description here

I am going crazy for this problem but I dont be able to solve. Can you help me?

8
  • did you check this in xcode preview by adding different devices , that would help you to chk what exact constraint you missing : here is the link to know more pinkstone.co.uk/how-to-preview-storyboards-in-interface-builder Commented Dec 18, 2016 at 19:52
  • I just tried this but I see the same on simulator and I dont understand what is the error Commented Dec 18, 2016 at 20:01
  • have you tried to add constraints for collection view? so it will be always inside superview Commented Dec 18, 2016 at 20:02
  • hoe do I set constraints for collectionview? Commented Dec 18, 2016 at 20:04
  • how do I set constraints for collectionview? Commented Dec 18, 2016 at 20:05

1 Answer 1

1

You need to extend your ViewController1 to conform to UICollectionViewDelegateFlowLayout protocol. And implement methods to set size for each collection view cell like so:

class ViewController1: UICollectionViewController, UICollectionViewDelegateFlowLayout {
override func viewDidLoad() {

    super.viewDidLoad()

    self.collectionView?.delegate = self
    self.collectionView?.dataSource = self
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    print(self.collectionView!.frame)
}

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

    return 5
}

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "P", for: indexPath)

    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
    return 0
}

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

It has hard coded values, but you should get the idea. Collection View width should be the max size for each item, so they won't get outside the box.

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.