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

时间:2017-02-09 05:42:17

标签: ios swift uibutton autolayout uilabel

我创建了一个自定义集合视图单元格。它有 UILabel UIView UIButton ,然后是 UITextView

要求是

  1. UILabel的文字大小可以是任何东西。基于此,它的内容大小标签必须重新调整大小。它运作正常。
  2. 可以向UIView添加任意数量的视图。基于此,UIView必须重新调整大小
  3. UIButton只是一个普通的按钮。当我们点击它时,下面的TextView将切换。
  4. 我应用的约束是:

    enter image description here

1 个答案:

答案 0 :(得分:-1)

使用此方法并计算适合您的大小并将其返回。

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*/)

}

不要忘记从viewController

延长UICollectionViewDelegateFlowLayout
相关问题