如何解决UICollectionView 项目间距不是恒定的
我正在尝试显示一个集合视图,其中每个单元格在边框内显示一个字符串。我已将方向设置为垂直。因此,每行中可以有多个具有动态大小的单元格。但是单元格以不同的项目间空间显示。
收藏视图的当前状态:
我的收藏视图配置如下:
func collectionView(_ collectionView: UICollectionView,layout collectionViewLayout: UICollectionViewLayout,sizeForItemAt indexPath: IndexPath) -> CGSize {
if(collectionView == tagsCollectionView)
{
if(self.propertyTypes.count > 0)
{
if(indexPath.row == 0)
{
return CGSize(width: "You searched for properties in: ".width(withConstrainedHeight: 25,font: UIFont(name: "Avenir-Book",size: 13)!),height: 30)
}
return CGSize(width: self.propertyTypes[indexPath.row - 1].width(withConstrainedHeight: 25,height: 30)
}
return CGSize(width: 0,height: 0)
}
return "String".size(withAttributes: nil)
}
func collectionView(_ collectionView: UICollectionView,minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 10
}
func collectionView(_ collectionView: UICollectionView,minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 5
}
但是,当方向为 HORIZONTAL 时,集合视图绝对可以正常工作。
解决方法
您可以使用 pod:https://github.com/mischa-hildebrand/AlignedCollectionViewFlowLayout 来实现您的功能。
您可以使用此库实现左、右、对齐等。
像这样使用:
let alignedFlowLayout = collectionView?.collectionViewLayout as? AlignedCollectionViewFlowLayout
alignedFlowLayout?.horizontalAlignment = .left
alignedFlowLayout?.verticalAlignment = .top
检查图书馆了解更多详细信息。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。