I have a colors collectionView like this:
But the space between rows in my collection view is too big. I try to set minimumInteritemSpacing
, minimumLineSpacing
, sectionInset
and try some way in stackoverflow but none of them are worked. Can anyone help me to fix this issue? Here's my code:
import UIKit
let reuseIdentifier = "ColorCell"
@objc protocol ColorMessageSettingDelegate{
optional func changeColorText(colorPicker: ColorCollectionViewController, color: UIColor)
}
class ColorCollectionViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, ColorMessageSettingDelegate, UIScrollViewDelegate, UICollectionViewDelegateFlowLayout {
var colors: [UIColor] = COLORS
var selectedColor = -1
var selectedImageColor = UIImage(named: "ic_selected")
var delegate: ColorMessageSettingDelegate?
@IBOutlet var pageControl: UIPageControl!
@IBOutlet var colorCollectionView: UICollectionView!
//var viewHeight: CGFloat = SCREEN_HEIGHT/2
override func viewDidLoad() {
super.viewDidLoad()
let viewLayout = UICollectionViewFlowLayout()
viewLayout.scrollDirection = UICollectionViewScrollDirection.Vertical
viewLayout.minimumInteritemSpacing = 2
viewLayout.minimumLineSpacing = 2
viewLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0)
self.colorCollectionView = UICollectionView(frame: self.colorCollectionView.frame, collectionViewLayout: viewLayout)`enter code here`
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//#warning Incomplete method implementation -- Return the number of sections
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
//#warning Incomplete method implementation -- Return the number of items in the section
return colors.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as ColorCollectionViewCell
cell.backgroundColor = colors[indexPath.row]
if (indexPath.row == selectedColor) {
//cell.iconStamp
cell.iconStamp.hidden = false;
}
else {
cell.iconStamp.hidden = true;
}
let pages = floor(collectionView.contentSize.width / collectionView.frame.width) + 1
pageControl.numberOfPages = Int(pages)
return cell
}
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
selectedColor = indexPath.row
collectionView.reloadData()
delegate?.changeColorText!(self, color: colors[selectedColor])
}
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
let pageWidth = colorCollectionView.frame.width
let currentPage = colorCollectionView.contentOffset.x / pageWidth
if (0 != fmod(currentPage, 1)){
pageControl.currentPage = Int(currentPage) + 1
} else {
pageControl.currentPage = Int(currentPage)
}
}
@IBAction func changeStampPage(sender: AnyObject) {
var x: CGFloat = CGFloat(pageControl.currentPage) * colorCollectionView.frame.size.width
colorCollectionView.setContentOffset(CGPointMake(x, 0), animated: true)
}
}