如何确定UICollectionView中项目之间的间距?

时间:2015-03-20 04:35:55

标签: ios uicollectionviewlayout

我有一个颜色collectionView,如下所示: enter image description here
但是我的集合视图中的行之间的空间太大了。我尝试设置minimumInteritemSpacingminimumLineSpacingsectionInset并在stackoverflow中尝试某种方式,但它们都没有工作。任何人都可以帮我解决这个问题吗?这是我的代码:

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)
    }
}

3 个答案:

答案 0 :(得分:0)

如果您使用storyboard设计布局,请选择Size inspector

使用UICollectionView

答案 1 :(得分:0)

您的代码存在的问题是您正在创建流布局并在您在代码中创建的集合视图上进行设置,但从不添加到您的视图中。你也有一个集合视图出口,所以我假设你在那里制作了集合视图(在故事板中)。删除这一行,

self.colorCollectionView = UICollectionView(frame: self.colorCollectionView.frame, collectionViewLayout: viewLayout)

您的viewDidLoad方法应该如下所示,

override func viewDidLoad() {
    super.viewDidLoad()

    let viewLayout = self.colorCollectionView.collectionViewLayout as UICollectionViewFlowLayout // you should already have a layout object that came in with the collection view. This gets a reference to it
    viewLayout.scrollDirection = .Vertical
    viewLayout.minimumInteritemSpacing = 2
    viewLayout.minimumLineSpacing = 2
}

如果你想让它全部为零,你不需要设置sectionInset;这是默认值。

答案 2 :(得分:0)

CollectionView Delegates根据任务完成自定义单元格的工作。首先执行UICollectionViewDelegate,UICollectionViewDelegateFlowLayout并使用以下委托方法:

#pragma mark UICollectionView Delegates
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(100,100);
}

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

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 2.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 1.0;
}