UICollectionView Cells不是并排的

时间:2018-02-10 13:44:03

标签: ios swift uicollectionview cell

我有UICollectionView 2行单元格。  只要细胞长度中的文字但细胞不是并排的。

我有UICollectionView 2行单元格。  只要细胞长度中的文字但细胞不是并排的。

enter image description here

如何解决这个问题。

这是我的代码。

{{1}}

3 个答案:

答案 0 :(得分:0)

添加此方法:

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

答案 1 :(得分:0)

我相信你只需要在setUpViews()函数中添加“override”:

 class BaseCell: UICollectionViewCell {

override init(frame: CGRect) {
    super.init(frame: frame)
    setUpViews()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")

}


func setUpViews() {
    backgroundColor = .blue

}

}
class FriendCell: BaseCell {

     override func setUpViews() {

        addSubview(profileImageView)
        addSubview(dividerLineView)

        setUpContainerView()

        profileImageView.image = UIImage(named: "boy.png")


        addConstraintsWithformat(format: "H:|-12-[v0(68)]", views: profileImageView)
        addConstraintsWithformat(format: "V:[v0(68)]", views: profileImageView)

        addConstraint(NSLayoutConstraint(item: profileImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))

        addConstraintsWithformat(format: "H:|-82-[v0]|", views: dividerLineView)
        addConstraintsWithformat(format: "V:[v0(1)]|", views: dividerLineView)

}

并添加这样的东西来尝试:

private func setUpContainerView() {
        let containerView = UIView()
        addSubview(containerView)

        addConstraintsWithformat(format: "H:|-90-[v0]|", views: containerView)
        addConstraintsWithformat(format: "V:[v0(50)]|", views: containerView)
        addConstraint(NSLayoutConstraint(item: containerView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))



    }

答案 2 :(得分:0)

请使用自定义布局来对齐集合视图单元格

使用下面的库:: https://github.com/mokagio/UICollectionViewLeftAlignedLayout

使用以下代码设置集合视图布局

    let layout = UICollectionViewLeftAlignedLayout.init()
    self.collectionView.collectionViewLayout = layout