iOS UICollectionView estimatedItemSize 自定适应高度(一)

iOS UICollectionView 计算高度(二)
UICollectionViewFlowLayout 中有属性estimatedItemSize(UITableView也有这个属性,操作比UICollectionView简单) 默认是0,只要设置不是0 就会调用UICollectionViewCell当中的preferredLayoutAttributesFitting方法。

xib约束 多个label自适应(一个label只需要上下左右约束即可)
iOS UICollectionView estimatedItemSize 自定适应高度(一)_第1张图片
黄色label约束
iOS UICollectionView estimatedItemSize 自定适应高度(一)_第2张图片
蓝色label约束
iOS UICollectionView estimatedItemSize 自定适应高度(一)_第3张图片
高度的约束
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
    }
}

你可能感兴趣的:(iOS UICollectionView estimatedItemSize 自定适应高度(一))