Adjust insets of a specific section in UICollectionView

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Adjust insets of a specific section in UICollectionView



insetForSectionAtIndex (on DelegateFlowLayout) enables one to set insets for all cells within a section



sectionInset (on FlowLayout) enables one to set insets that applies to all sections.



However, I am looking for a way of applying insets to only one specific section - is this possible?




2 Answers
2



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


UICollectionViewDelegateFlowLayout



For Swift 4


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;






I tried this earlier, and it seems to apply the inset to all elements in the section. Is there a way I can apply it to the entire section, not each element?
– Gaurav Sharma
Aug 10 '16 at 13:48





@GauravSharma This method apply the inset to the entire section, that's the reason it gives it an section Int.
– pableiros
Aug 10 '16 at 14:28


Int





just tried again - same result - it applies the insets to each cell in the section, rather than the section.
– Gaurav Sharma
Aug 10 '16 at 16:42






@GauravSharma and what is the difference? Do you want to apply the inset to the header section and footer section?
– pableiros
Aug 10 '16 at 16:53





Nope, I want to inset the entire section (not each item in the section).
– Gaurav Sharma
Aug 10 '16 at 17:00



for swift 4 is named:


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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard