简体   繁体   English

UICollectionView:水平滚动的动态单元格宽度

[英]UICollectionView: dynamic cell width for horizontal scrolling

I have a collection view with several labels inside located in one line. 我有一个集合视图,其中一行内有多个标签。 This collection view has to have horizontal scrolling and cell width calculated dynamically based on the label's sizes. 该集合视图必须具有水平滚动和根据标签大小动态计算的像元宽度。 I found the solution on how to make cell's width dynamic here: Dynamic cell width of UICollectionView depending on label width 我在这里找到了有关如何使单元格宽度动态化的解决方案: UICollectionView的动态单元格宽度取决于标签宽度

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    return (labels[indexPath.row] as NSString).size(attributes: nil)
}

But this method is only called when the class conforms to UICollectionViewDelegateFlowLayout which has vertical scrolling direction that doesn't suit me. 但是,仅当类符合UICollectionViewDelegateFlowLayout时才调用此方法,UICollectionViewDelegateFlowLayout的垂直滚动方向不适合我。

How can I make dynamic cell width for default layout with horizontal scrolling? 如何通过水平滚动为默认布局设置动态单元格宽度?

Have you tried setting the scroll direction of your UICollectionViewFlowLayout to horizontal? 您是否尝试过将UICollectionViewFlowLayout的滚动方向设置为水平?

yourFlowLayout.scrollDirection = .horizontal

Apple Documentation for this Apple文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM