带有文本的Swift UICollectionViewCells具有奇数间距

时间:2017-09-03 18:07:15

标签: swift uicollectionview

以下是每个单元格中包含文字的UICollectionViewenter image description here

以下是一些归因于间距的代码:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return CGSize(width: collectionView.frame.width / 4.0, height: 20)
}

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

class ViewThatContainsCollectionView: UIView {

    var delegate: MyDelegate! {
        didSet {
            self.collection_view.delegate = self.delegate
            self.collection_view.dataSource = self.delegate
        }
    }

    var collection_view: UICollectionView!

    let array_of_text: String = [
        "Public",
        "My Crowds",
        "Invite Only",
        "Group"
    ]

    private let title_label: UILabel = {
        let label = UILabel()
        label.text = "Type:"
        label.textColor = .EVO_text_gray
        label.font = UIFont(name: "OpenSans", size: 18)
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    init() {
        super.init(frame: .zero)
        self.setUpContainerUI()

        let layout = UICollectionViewFlowLayout()

        self.collection_view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        self.collection_view.register(MyCustomCell.self, forCellWithReuseIdentifier: MyCustomCell.reuse_identifier)
        self.collection_view.backgroundColor = .clear
        self.collection_view.translatesAutoresizingMaskIntoConstraints = false

        self.addSubview(self.title_label)
        self.addSubview(self.collection_view)

        self.title_label.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 10).isActive = true
        self.title_label.topAnchor.constraint(equalTo: self.topAnchor, constant: 6).isActive = true

        self.collection_view.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        self.collection_view.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true
        self.collection_view.topAnchor.constraint(equalTo: self.title_label.bottomAnchor, constant: 5).isActive = true
        self.collection_view.heightAnchor.constraint(equalToConstant: 20).isActive = true
    }

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

    private func setUpContainerUI() {
        self.backgroundColor = .white
        self.layer.borderWidth = 1
        self.layer.borderColor = .lightGray
        self.layer.cornerRadius = 5
        self.layer.masksToBounds = true
        self.translatesAutoresizingMaskIntoConstraints = false
    }

}

class MyCustomCell: UICollectionViewCell {

    static let reuse_identifier = "EventCreationMethodCell"

    let label: UILabel = {
        let label = UILabel()
        label.textColor = .lightGray
        label.textAlignment = .center
        label.font = UIFont(name: "OpenSans", size: 15)
        return label
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.contentView.backgroundColor = .clear

        self.addSubview(label)
        self.label.frame = self.bounds
    }

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

}

如果我在每个单元格中使用相等长度的文本,则每个单元格之间的间距看起来很好。这里,文本的长度不同,因此中间的单元格看起来彼此靠近,外部单元格看起来有点远。无论如何,让细胞出现"出现"间隔均匀?如果需要更多代码,请告诉我。感谢。

0 个答案:

没有答案