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
Swift Error Using Initialized Properties in Expressions Before Super.Init
Passing a Variable Through a Segue? Xcode 8 Swift 3
How to Save a Custom Class as an Attribute of a Coredata Entity in Swift 3
Images Inaccessible from Asset Catalog in a Swiftui Framework
Cannot Invoke Initializer for Type 'Sqlite3_Destructor_Type'
Http Request Swift Providing Parameters
Can Not Cast Value of Type Nstaggedpointerstring to Nsdictionary
Making a Http Post Request in Swift 2
Code Highlighting in Latex for Swift
How to Calculate the Energy Per Bin in a Dft
Swiftui: Localizedstringkey with Indices
How to Detect the 2D Images Using Arkit and Realitykit
If-Let Any to Rawrepresentable<String>
Saving Dictionary into Nsuserdefaults
Why Doesn't Swift Force My Designated Initializer to Call Super
How to Capitalize First Word in Every Sentence with Swift
Swift Generics: Non-Nominal Type Does Not Support Explicit Initialization