Swift - UICollectionView动态高度

时间:2018-05-31 06:32:40

标签: swift

我试图用单元格数来改变UICollectionView的高度。

单元格高度为25,单元格之间的行间距为10。

我想允许用户按下添加更多单元格按钮,当它被点击时,我想增加UICollectionView的高度。

以下是我的代码:

import UIKit

class TestController: UIViewController, UICollectionViewDataSource {

    let collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .white
        let cellWidth = UIScreen.main.bounds.width - 40
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 10
        layout.itemSize = CGSize(width: cellWidth, height: 25)
        collectionView.collectionViewLayout = layout
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        return collectionView
    }()

    var numberOfCells = 1

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return numberOfCells
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! Cell
        return cell
    }

    let addMoreCellButton: UIButton = {
        let button = UIButton()
        button.setTitle("Add more cell", for: .normal)
        button.addTarget(self, action: #selector(addMoreCell), for: .touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = falase
        return button
    }()

    @objc func addMoreCell() {
        numberOfCells += 1
        collectionView.heightAnchor.constraint(equalToConstant: CGFloat(numberOfCells * 35 - 10)).isActive = true
        collectionView.reloadData()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.dataSource = self
        collectionView.register(Cell.self, forCellWithReuseIdentifier: "cell")

        view.addSubview(collectionView)
        collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0).isActive = true
        collectionView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true
        collectionView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: 0).isActive = true
        collectionView.heightAnchor.constraint(equalToConstant: 25).isActive = true

        view.addSubview(addMoreCellButton)
        collectionView.topAnchor.constraint(equalTo: collectionView.bottomAnchor, constant: 20).isActive = true
        collectionView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 0).isActive = true

    }

}

在函数addMoreCell()中我改变了高度,但是我在控制台中收到了这条消息。

[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x60400028d020 UICollectionView:0x7fddfe206000.height == 25   (active)>",
"<NSLayoutConstraint:0x60400028aeb0 UICollectionView:0x7fddfe206000.height == 60   (active)>"
)

Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x60400028aeb0 UICollectionView:0x7fddfe206000.height == 60   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

有人能告诉我如何解决这个问题吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我做了类似的事情,其中​​更多的增加收集高度。使用Dynamic collectionView只需将此类提供给collectionView。重新加载collectionView动态增加collectionView高度,如果约束是正确的,则滚动下面的内容。希望它有所帮助。

class DynamicCollectionView: UICollectionView {

    override func layoutSubviews() {
        super.layoutSubviews()
        if bounds.size != intrinsicContentSize {
            invalidateIntrinsicContentSize()
        }
    }

    override var intrinsicContentSize: CGSize {
        return contentSize
    }
}

//另一个补丁

func collectionView(_ collectionView: UICollectionView,
                        numberOfItemsInSection section: Int) -> Int {
    return  isAllCategoriesShowned ? homeViewModel.categories.count:   numberOfCategoriesShownByDefault
  }