Pav*_*lov 11 iphone xcode ios uicollectionview swift
考虑以下情况.我有一个UICollectionView(内部UICollectionViewController),看起来几乎一样UITableView(我不使用的原因UITalbeView是因为我在布局上有非数据视图,我不想管理和搞乱我IndexPath).为了实现我设置的自动调整单元estimatedItemSize,类似的东西:
layout.estimatedItemSize = CGSize(width: self.view.bounds.size.width, height: 72)
Run Code Online (Sandbox Code Playgroud)
另外,在我的单元格中,我有布局属性:
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
layoutAttributes.bounds.size.height = systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
return layoutAttributes
}
Run Code Online (Sandbox Code Playgroud)
所以,通过这样做,我得到了UITableView与自动调整一样的精确布局.而且效果很好.
现在,我正在尝试添加标题并将其固定在滚动到该部分的顶部,如下所示:
layout.sectionHeadersPinToVisibleBounds = false
Run Code Online (Sandbox Code Playgroud)
但布局进入了奇怪的状态,我到处都有毛刺,细胞相互重叠,标题有时不会粘住.
更新:
视图控制器和单元的代码:
class ViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
let layout = collectionView?.collectionViewLayout as! UICollectionViewFlowLayout
layout.sectionHeadersPinToVisibleBounds = true
layout.estimatedItemSize = CGSize(width: collectionView?.bounds.size.width ?? 0, height: 36) // enables dynamic height
}
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return 10
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 10
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as! CustomCell
cell.heightConstraint.constant = CGFloat(indexPath.row * 10 % 100) + 10 // Random constraint to make dynamic height work
return cell
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
return collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "Header", for: indexPath)
}
class CustomCell : UICollectionViewCell {
let identifier = "CustomCell"
@IBOutlet weak var rectangle: UIView!
@IBOutlet weak var heightConstraint: NSLayoutConstraint!
override func awakeFromNib() {
translatesAutoresizingMaskIntoConstraints = false
}
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes {
layoutAttributes.bounds.size.height = systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
return layoutAttributes
}
Run Code Online (Sandbox Code Playgroud)
视频中滞后的详细信息:https://vimeo.com/203284395
| 归档时间: |
|
| 查看次数: |
2939 次 |
| 最近记录: |