The World’s Largest Online Community for Developers

'; ios - UICollectionView full width and height problem - LavOzs.Com

UICollectionView width is not full width, One cell per row


private func setCollectionView() {

    let layout = UICollectionViewFlowLayout()
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    layout.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    layout.scrollDirection = .horizontal

    collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
    collectionView.showsHorizontalScrollIndicator = false
    collectionView.translatesAutoresizingMaskIntoConstraints = false
    collectionView.isPagingEnabled = true
    collectionView.register(ImageCollectionViewCell.self, forCellWithReuseIdentifier: "cell")


UICollectionView -> Constraint

        view.leadingAnchor.constraint(equalTo: collectionView.leadingAnchor),
        view.trailingAnchor.constraint(equalTo: collectionView.trailingAnchor),
        view.topAnchor.constraint(equalTo: collectionView.topAnchor),
        view.bottomAnchor.constraint(equalTo: collectionView.bottomAnchor)


By adding UICollectionViewFlowLayoutDelegate

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


class ImageCollectionViewCell: UICollectionViewCell {
    private let imageView: UIImageView = {
        let img = UIImageView()
        img.contentMode = .scaleAspectFill
        img.translatesAutoresizingMaskIntoConstraints = false
        return img

ImageCollectionViewCell -> imageView Constraint


        leadingAnchor.constraint(equalTo: imageView.leadingAnchor),
        trailingAnchor.constraint(equalTo: imageView.trailingAnchor),
        topAnchor.constraint(equalTo: imageView.topAnchor),
        bottomAnchor.constraint(equalTo: imageView.bottomAnchor)

UICollectionView FlowLayout Warning

The item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.

The relevant UICollectionViewFlowLayout instance is , and it is attached to ; layer = ; contentOffset: {0, -44}; contentSize: {0, 0}; adjustedContentInset: {44, 0, 34, 0}> collection view layout: .

This is Keep Happening

UICollectionView Warning Fix

    if #available(iOS 11.0, *) { collectionView.contentInsetAdjustmentBehavior = .never } 
    else { automaticallyAdjustsScrollViewInsets = false } 

Scale to fill was giving bigger image than UIImageView size

    img.clipsToBounds = true
Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
UICollectionView loaded from Storyboard is NOT resizing correctly
UICollectionView as UITableView subview flow layout loop
Issues with casting celltypes to dequeue
Why is UICollectionView insetForSectionsAtIndex not being called?
Programmatically placing collectionView in view
UICollectionView destroyed in UITableViewCell
Changing frame and contentInset of UICollectionView in layoutSubviews() using IGListKit
How to set height on UICollectionView / keep height of UICollectionView from expanding in simulator