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

iOS UICollectionView 计算高度(二)
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
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 因为要结局swift3.0中引用snapKit的问题,看到一篇介绍Xcode8,swift3变化的文章,觉得很详细...
    uniapp阅读 10,034评论 0 12
  • iOS流布局UICollectionView系列一——初识与简单使用UICollectionView 一、简介 U...
    我是啊梁阅读 13,562评论 3 10
  • 一杯酒 我敬你 感谢你出现在我的生命里 曾带给我的感动和欢喜 第二杯 我也敬你 愿你前程似锦从此豁达 有酒有肉有人...
    周围亮晶晶阅读 1,358评论 1 3
  • 下面的代码用到了《66-偏函数应用:简单的图形窗口》图形窗口上的按钮有个command选项,其实它就是一个函数。如...
    凯茜的老爸阅读 4,711评论 2 4