Я пытаюсь изменить высоту 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 () Iизменил высоту, но я получаю это сообщение в консоли.
[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.
Может кто-нибудь сказать мне, как решить эту проблему?Спасибо!