BigBoy1337
BigBoy1337

Reputation: 4963

How to align UICollectionView cell to UICollectionView bounds

This has been asked several times and I have tried to include aspects of all answers in my code - which still does not work - so there may be issues with methods/characteristics becoming obsolete in the newest iOS or with me just doing things wrong. But here is where I'm at:

enter image description here

The teal is background color for the CollectionView. Then there is a PostCell with a pink background which is covered with a blue image view on top of it to which I set an image from a url (the video thumbnail on top) as well as a pink textView containing notes.

What I want is to make the PostCell adhere to the top and bottom of the collection View so that you can no longer see the teal section.

Here are the constraints I have set as well as the view hierarchy:

enter image description here

In my TagViewController - the controller for this view, I have:

class TagViewController: UIViewController,  UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {

    ....

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        //top, left, bottom, right
        return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.automaticallyAdjustsScrollViewInsets = false
        collectionView.contentInsetAdjustmentBehavior = .never
    }
    ....
}

The insetForSectionAt method and the methods I set in viewDidLoad are from the answers to similar questions I have found on stackOverflow so far such as here: UICollectionView adds top margin and here: iOS:Setting UICollectionView cell to view size

Upvotes: 0

Views: 709

Answers (1)

ericl
ericl

Reputation: 321

Have you tried the 'sizeForItemAt indexPath' method ?

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

Upvotes: 1

Related Questions