简体   繁体   English

使用自动布局的自定义集合视图单元格的动态高度

[英]Dynamic height for custom collection view cell by using autolayouts

I had created a custom collection view cell. 我创建了一个自定义集合视图单元格。 It has a UILabel , UIView , UIButton and then UITextView . 它具有UILabelUIViewUIButtonUITextView

Requirements are : 要求是

  1. UILabel's text size can be anything. UILabel的文本大小可以是任何值。 Based on that it's content size label must re-size . 基于此,其内容大小标签必须重新调整大小 It is working properly. 它工作正常。
  2. any number of views can be added to UIView. 可以将任意数量的视图添加到UIView。 Based on that this UIView must be re-size . 基于此,UIView必须重新调整大小
  3. UIButton is just a normal button. UIButton只是一个普通的按钮。 when we click on it, the below TextView will toggle. 当我们单击它时,下面的TextView将切换。

The constraints that I applied are: 我应用的约束是:

在此处输入图片说明

use this method and calculate the size that is appropriate for you and return it. 使用此方法并计算出适合您的尺寸并返回。

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let Labell : UILabel = UILabel()

    Labell.text =   self.items[indexPath.item]
    let labelTextHeight =   Labell.intrinsicContentSize.height
    //calculate size for UIView, UIButton, TextView and then add it
    return CGSize(width: /*fix width*/, height: labelTextHeigh + /*your UIView height and other height*/)

}

don't forget to extend your viewController from UICollectionViewDelegateFlowLayout 不要忘记延长您viewControllerUICollectionViewDelegateFlowLayout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM