[英]UICollectionView cell fixed width and dynamic height using constraints in swift
I have one collectionview and a cell inside that which is having a fixed width and dynamic height based on its contents.I am using autolayout constraints , my code is below.我有一个 collectionview 和一个单元格,里面有一个基于其内容的固定宽度和动态高度。我正在使用自动布局约束,我的代码如下。
I have already tried to set all types of auto constraints inside the cell and inside the view .我已经尝试在单元格内和视图内设置所有类型的自动约束。
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 5
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! ViewControllerCell
cell.lbl_text.text = "djs sdfs sdfs dsfsfd gdgfd dfgd df "
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if let flowLayout = coll_main?.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize
}
}
In cell class method:
override func awakeFromNib() {
super.awakeFromNib()
self.contentView.autoresizingMask = [UIView.AutoresizingMask.flexibleHeight]
}
override func layoutSubviews() {
self.layoutIfNeeded()
}
i get cell width fix without i am set that width.我得到了单元格宽度修复,而我没有设置那个宽度。 how may this issue solve?这个问题如何解决?
here screenshots.这里截图。
您需要确保您的 CollectionView 的Estimate Size
设置为None
。
You can use collectionView delegate您可以使用 collectionView 委托
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width:, height:)
}
for this you need to extend UICollectionViewDelegateFlowLayout
为此,您需要扩展UICollectionViewDelegateFlowLayout
You have to use UICollectionViewDelegateFlowLayout
and implement sizeForItemAt
like following.您必须使用UICollectionViewDelegateFlowLayout
并实现sizeForItemAt
如下所示。
// MARK: - UICollectionViewDelegateFlowLayout -
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let sectionInset = (collectionViewLayout as! UICollectionViewFlowLayout).sectionInset
// Here you can set dynamic height for the individual cell according to your content size.
let referenceHeight: CGFloat = 100 // Approximate height of your cell
let referenceWidth = collectionView.safeAreaLayoutGuide.layoutFrame.width
- sectionInset.left
- sectionInset.right
- collectionView.contentInset.left
- collectionView.contentInset.right
return CGSize(width: referenceWidth, height: referenceHeight)
}
Here is an articale about it, you can follow this .这是一篇关于它的文章,你可以关注这个。
Hope it helps you.希望对你有帮助。 Happy Coding快乐编码
在您的集合视图中添加此属性或取消选中笔尖中的 PrefetchingEnabled 属性。
collectionView.isPrefetchingEnabled = false
Implement UICollectionViewDelegateFlowLayout method for collection cell size.为集合单元格大小实现 UICollectionViewDelegateFlowLayout 方法。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
Then go to the storyboard and in size inspector make collectionView Estimate size to None然后转到故事板并在大小检查器中将 collectionView估计大小设置为 None
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.