iOS UICollectionView estimatedItemSize 自定適應高度(一)
UICollectionViewFlowLayout 中有屬性estimatedItemSize(UITableView也有這個屬性,操作比UICollectionView簡單) 預設是0,只要設定不是0 就會呼叫UICollectionViewCell當中的preferredLayoutAttributesFitting方法。
xib約束 多個label自適應(一個label只需要上下左右約束即可)

黃色label約束

藍色label約束

高度的約束
xib搭建UICollectionViewCell 有兩種方法可以自適應高度
1、使用SnapKit 給contentView約束 (使用系統約束沒寫出來)(感謝向陽我大哥)(如果只在xib中處理不需要在cell中操作 請大神指教)
//self.contentView.snp.makeConstraints { (make) in //make.left.right.top.bottom.equalTo(0) //make.width.equalTo(UIScreen.main.bounds.size.width) //}
2、重寫preferredLayoutAttributesFitting
override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes { self.setNeedsLayout() self.layoutIfNeeded() let size = self.contentView.systemLayoutSizeFitting(layoutAttributes.size) var cellFrame = layoutAttributes.frame cellFrame.size.height = size.height layoutAttributes.frame = cellFrame return layoutAttributes }
UICollectionViewCell程式碼
// //GAEstimatedCell.swift //YYFramework // //Created by 侯佳男 on 2018/12/26. //Copyright © 2018年 houjianan. All rights reserved. // import UIKit import SnapKit class GAEstimatedCell: UICollectionViewCell { @IBOutlet weak var l: UILabel! @IBOutlet weak var l1: UILabel! override func awakeFromNib() { super.awakeFromNib() // 002 //self.contentView.snp.makeConstraints { (make) in //make.left.right.top.bottom.equalTo(0) //make.width.equalTo(UIScreen.main.bounds.size.width) //} } // 001 override func preferredLayoutAttributesFitting(_ layoutAttributes: UICollectionViewLayoutAttributes) -> UICollectionViewLayoutAttributes { self.setNeedsLayout() self.layoutIfNeeded() let size = self.contentView.systemLayoutSizeFitting(layoutAttributes.size) var cellFrame = layoutAttributes.frame cellFrame.size.height = size.height layoutAttributes.frame = cellFrame return layoutAttributes } }
UIViewController程式碼
// //GAEstimatedCollectionViewController.swift //YYFramework // //Created by 侯佳男 on 2018/12/26. //Copyright © 2018年 houjianan. All rights reserved. // import UIKit class GAEstimatedCollectionViewController: UIViewController { lazy var collectionView: UICollectionView = { let layout = UICollectionViewFlowLayout() layout.estimatedItemSize = CGSize(width: kScreenWidth, height: 100) layout.minimumLineSpacing = 0 layout.minimumInteritemSpacing = 10 let v = UICollectionView(frame: self.view.bounds, collectionViewLayout: layout) v.delegate = self v.dataSource = self v.backgroundColor = UIColor.lightGray return v }() var data: [String] = ["YYBaseCollectionViewControllerYYBaseCollectionViewControllerYYBaseCollectionViewController", "2233", "YYBaseCollectionViewControllerYYBaseCollectionViewController", "YYBaseCollectionViewControllerYYBaseCollectionViewController", "YYBaseCollectionViewControllerYYBaseCollectionViewControllerYYBaseCollectionViewController", "2233", "YYBaseCollectionViewControllerYYBaseCollectionViewController", "YYBaseCollectionViewControllerYYBaseCollectionViewController", "YYBaseCollectionViewControllerYYBaseCollectionViewControllerYYBaseCollectionViewController", "2233", "YYBaseCollectionViewControllerYYBaseCollectionViewController", "YYBaseCollectionViewControllerYYBaseCollectionViewController", "YYBaseCollectionViewControllerYYBaseCollectionViewControllerYYBaseCollectionViewController", "2233", "YYBaseCollectionViewControllerYYBaseCollectionViewController", "YYBaseCollectionViewControllerYYBaseCollectionViewController"] override func viewDidLoad() { super.viewDidLoad() self.view.addSubview(collectionView) collectionView.snp.makeConstraints { (make) in make.edges.equalTo(self.view) } collectionView.yy_register(nibName: GAEstimatedCell.identifier) } } extension GAEstimatedCollectionViewController: UICollectionViewDelegate, UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: GAEstimatedCell.identifier, for: indexPath) as! GAEstimatedCell cell.l.text = data[indexPath.row] cell.l1.text = data[indexPath.row] return cell } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return data.count } }