ios - 自动调整 collectionview 单元格不能以最小的行间距正确显示

标签 ios swift3 uicollectionview autosize

我有自动调整大小的单元格 Collection View ,我需要在其中水平显示数据。一切正常,但我需要在两个单元格之间放置 5px 的间距,因此 minimumLineSpacing 属性和问题开始了 由于行间距,我的最后一个单元格无法正确显示。这是图片 enter image description here

这是代码

extension TopicVC : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 5.0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HastagCell.identifier, for: indexPath) as! HastagCell
        let index = indexPath.row % arrClrFollowTopic.count
        cell.btnHashTag.backgroundColor = Color.custom(hexString: arrClrFollowTopic[index], alpha: 1.0).value
        if indexPath.row % 3 == 0
        {
            cell.btnHashTag.setTitle(strTitle: "#Testing 123545")
        }
        else
        {
            cell.btnHashTag.setTitle(strTitle: "#Testing")
        }
        return cell;
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    }
}

ViewDidLoad

override func viewDidLoad() {
        super.viewDidLoad()


        self.objCollectionView.register(HastagCell.nib, forCellWithReuseIdentifier: HastagCell.identifier)



        if let collectionViewLayout = self.objCollectionView.collectionViewLayout as? UICollectionViewFlowLayout{
            collectionViewLayout.estimatedItemSize = CGSize.init(width: 1.0, height: 1.0)
        }

        self.objCollectionView.delegate = self
        self.objCollectionView.dataSource = self
        // Do any additional setup after loading the view.
    }

My Cell design with autolayout

enter image description here

Collectionview layout design

enter image description here

最佳答案

您需要将布局的 minimumInteritemSpacing 值设置为与 minimumLineSpacing 相同。

所以添加:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
    return 5
}

关于ios - 自动调整 collectionview 单元格不能以最小的行间距正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51655690/

相关文章:

swift - 将类对象转换为不符合 NSCoding 的 NSData

ios - UICollectionViewCell contentView 填充

ios - 当选择另一个单元格按钮时取消选择 Collection View 单元格按钮

swift - 防止 UICollectionCell 在 UICollectionView 中居中

ios - 在 Alamofire 中发送 JSONObject

ios - 如何在 UITableViewCell (Swift) 中暂停 AVPlayer?

swift - 如何在 Swift 3 for Linux 中使用 Process()?

swift - 如何在 Swift 3 中输入别名方法?

ios - iOS7 的 UISearchBar 问题

ios - 如何使用 Xcode 4.2 导入用于 Objective-C 的 Google API 客户端库