Set Insets to the Collectionview Programmatically in Swift

Set insets to the CollectionView programmatically in Swift

Set the section inset and invalidate the layout.

let collectionViewLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout

collectionViewLayout?.sectionInset = ... // some UIEdgeInset

collectionViewLayout?.invalidateLayout()

Adjust insets of a specific section in UICollectionView

You must have to implement UICollectionViewDelegateFlowLayout to your class with this method:

For Swift 4, 5+

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

var edgeInsets = UIEdgeInsets()
if section == THE_SECTION_YOU_WANT {
// edgeInsets configuration stuff
}

return edgeInsets;
}

For Swift 3

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {

var edgeInsets = UIEdgeInsets()
if section == THE_SECTION_YOU_WANT {
// edgeInsets configuration stuff
}

return edgeInsets;

}

how to add Section Insect in collection view programmatically?

contentInset is NOT sectionInset. Here's how you can assign sectionInset.

if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.sectionInset = UIEdgeInsets(top: 60, left: 0, bottom: 0, right: 0)
}

Is there a way to add padding to a UICollectionView cell programmatically?

Use this method - optional func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets

Programmatically placing collectionView in view

because I subtract the height of the different elements on the view from the total height of the view

But you don’t know the total height of the view. That’s the problem.

You are calling loadCollectionView in viewDidLoad. That is way too early, because nothing has its correct frame yet. Therefore all your calculations to calculate the frame of the collection view, based on the frame of other things, are incorrect.

You could solve this by waiting until viewDidLayoutSubviews to call loadCollectionView, but don't. You shouldn't be calculating frames at all! Instead, do everything with autolayout and let the autolayout engine do the layout for you. That way, you can do the construction in viewDidLoad without any frame values coming into play.

UICollectionView - add bottom inset for entire collectionView

You can try

collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom:<#someValue#>, right: 0)

UICollectionView spacing margins

You can use the collectionView:layout:insetForSectionAtIndex: method for your UICollectionView or set the sectionInset property of the UICollectionViewFlowLayout object attached to your UICollectionView:

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
return UIEdgeInsetsMake(top, left, bottom, right);
}

or

UICollectionViewFlowLayout *aFlowLayout = [[UICollectionViewFlowLayout alloc] init];
[aFlowLayout setSectionInset:UIEdgeInsetsMake(top, left, bottom, right)];

Updated for Swift 5

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


Related Topics



Leave a reply



Submit