I am initializing a UICollectionView
as below , I want different line spacing for different section.
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
var baseCollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
baseCollectionView.backgroundColor = UIColor.clearColor()
baseCollectionView.translatesAutoresizingMaskIntoConstraints = false
baseCollectionView.showsVerticalScrollIndicator = false
baseCollectionView.delegate = self
baseCollectionView.dataSource = self
And then defining the layout methods as follows ,
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
{
switch section {
case 0:
return 10.0
case 1:
return 10.0
case 2 :
return 10.0
case 3:
return 10.0
case 4 :
return 0.0
case 5 :
return 0.0
case 6 :
return 0.0
case 7 :
return 0.0
default:
return 0.0
}
}
And,
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat
{
switch section {
case 0:
return 10.0
case 1:
return 10.0
case 2 :
return 10.0
case 3:
return 10.0
case 4 :
return 0.0
case 5 :
return 0.0
case 6 :
return 0.0
case 7 :
return 0.0
default:
return 0.0
}
}
And other delegates too, minimumInteritemSpacingForSectionAtIndex
is called but minimumLineSpacingForSectionAt
delegate method is not getting called , how to solve this , where am I wrong?
I have conformed to delegate protocols too.
class MyViewController: UIViewController , UICollectionViewDelegateFlowLayout , UICollectionViewDelegate , UICollectionViewDataSource
Here you go,
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
switch section {
case 0:
return CGSizeMake(140.0,self.contentView.frame.size.height)
case 1:
return CGSizeMake(140.0,self.contentView.frame.size.height)
case 2 :
return CGSizeMake(140.0,self.contentView.frame.size.height)
case 3 :
return CGSizeMake(140.0,self.contentView.frame.size.height)
case 4:
return CGSizeMake(self.contentView.frame.size.width,self.contentView.frame.size.height)
case 5:
return CGSizeMake(self.contentView.frame.size.width / 2.0,self.contentView.frame.size.height)
case 6:
return CGSizeMake(75.0, self.contentView.frame.size.height)
case 7:
return CGSizeMake(((self.contentView.frame.size.width - 40.0) / 3.0), 150.0)
default:
return CGSizeZero
}
}
For all the other people, actually the display design sent from my server is being used to set the various constants.