0

I have to Collection View in my view Controller. One is Showing the image and the other is showing the ticket details. Here is the sample image.

But I can't set the proper constraint. This is what I Do with code.

 let numberOfCells = 9
    let kCellHeight : CGFloat = 104
    let kLineSpacing : CGFloat = 2
    let kInset : CGFloat = 10

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        if collectionView == clnView {
            return CGSize.init(width: width, height: height)
        }else {
            return CGSize(width: (UIScreen.main.bounds.width - 2*kInset - kLineSpacing), height: kCellHeight)
        }
    }
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        var size = CGFloat()
        if collectionView == clnView {
            size = kLineSpacing
        }
        return size
    }
 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        var Insect = UIEdgeInsets()
        if collectionView == clnView {
            Insect = UIEdgeInsets(top: kInset, left: kInset, bottom: kInset, right: kInset)
        }
        return Insect
    }

But It's not showing properly. Look

iphone 5s

iPhone XR

Please Explain the Correct way. Thanks

Marwen Doukh
  • 1,946
  • 17
  • 26

1 Answers1

0

Your code simply defines how items are laid out inside the collection view.

Constraints should tell you where in your parent view collection view is placed, and how wide + high.

You must learn autolayout and do it step by step in your storyboard or xib for the view. With proper Y constraint, it should work as expected. If you are putting collectionview + other things inside scrollview, follow this answer.

Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89