UICollectionView中的单元格之间的零水平空间无法正常工作

时间:2016-05-06 00:21:27

标签: ios swift uicollectionview uicollectionviewcell uicollectionviewlayout

我是iOS开发的新手,似乎无法弄清楚为什么我的集合视图是间隔的。它几乎好像是一个" margin-right"如在CSS中已经应用于每个单元格。我设置了" Min Spacing"对于单元格和行为零,但仍然发生以下情况......

Screen shot of issue that is occurring

我故意将集合视图的背景颜色设置为蓝色,将单元格的背景颜色设置为红色,将标签的边框颜色设置为黑色以试图查看是什么。目标是不要看到任何蓝色的' (集合视图背景),所有。在配置集合视图时,我调整单元格的宽度以适应标签的固有宽度,但似乎其他单元格的大小也是如此。 x轴位置保持不变......我需要所有细胞聚集在一起。任何帮助将不胜感激!

当前的CollectionViewController代码......

import Foundation
import UIKit

class HomeViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

var menu = [
    ("NEWS"),
    ("NEIGHBORHOODS"),
    ("CULTURE"),
    ("DEVELOPMENT"),
    ("TRANSPORTATION"),
    ("CITIES")
]

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "Testing"
    self.navigationController?.navigationBarHidden = true

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return menu.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("UnderMenuCell", forIndexPath: indexPath) as! UnderMenuCollectionViewCell


    cell.underMenuLabel.text = menu[indexPath.row]
    let label_width = cell.underMenuLabel.intrinsicContentSize().width + 23

    cell.underMenuLabel.layer.zPosition = 1000000
    cell.underMenuLabel.layer.borderWidth = 2
    cell.underMenuLabel.layer.borderColor = UIColor.blackColor().CGColor

    cell.frame.size.width = label_width

    return cell

}

}

2 个答案:

答案 0 :(得分:0)

您可能需要为单元格设置布局属性以指定其宽度:

override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes? {
    let attributes = UICollectionViewLayoutAttributes(forCellWithIndexPath: indexPath)

    attributes.frame = yourFrameHere
    attributes.size = yourSizeHere

    return attributes
}

您是否尝试在cell.frame.size.width = label_width设置断点以查看实际设置的宽度?

答案 1 :(得分:0)

我最终找到了一种完美运作的方式:

import Foundation
import UIKit

class HomeViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

@IBOutlet weak var underMenuCollection: UICollectionView!

var menu = [
    ("NEWS"),
    ("NEIGHBORHOODS"),
    ("CULTURE"),
    ("DEVELOPMENT"),
    ("TRANSPORTATION"),
    ("CITIES")
]

override func viewDidLoad() {
    super.viewDidLoad()

    self.title = "Testing"
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)

    let scrollToIndexPath = NSIndexPath(forItem: 2, inSection: 0)
    self.underMenuCollection.scrollToItemAtIndexPath(scrollToIndexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)
}

func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return menu.count
}

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("UnderMenuCell", forIndexPath: indexPath) as! UnderMenuCollectionViewCell

    cell.underMenuLabel.text = menu[indexPath.row]

    return cell

}

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

    let size: CGSize!
    let testLabel = UILabel()
    let labelText = menu[indexPath.row]

    testLabel.text = labelText
    testLabel.font = UIFont(name: "Avenir Next Condensed", size: 17)
    testLabel.hidden = true

    size = CGSize(width: testLabel.intrinsicContentSize().width + 13, height: self.underMenuCollection.frame.height)

    return size

}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

    self.underMenuCollection.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.CenteredHorizontally, animated: true)

}

}

所以会发生什么呢?我创建了一个隐藏的标签,永远不会将evens添加到superview中,用文本填充它来计算内在宽度,然后设置CGSize。它完美地运作......

Picture of collection view to the left

Picture of collection view to the right

所有这一切中最重要的部分是根据字符串的宽度动态地在所有单元格之间保持相等的间距。