gpt4 book ai didi

Swift - UICollectionView 动态高度

转载 作者:行者123 更新时间:2023-11-30 11:27:08 26 4
gpt4 key购买 nike

我正在尝试通过单元格数量更改 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.

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

最佳答案

我做过类似的事情,更多地增加集合高度。使用 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
}

关于Swift - UICollectionView 动态高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50618020/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com