【问题标题】:Custom UICollectionViewCell class: how to change height in Swift 3自定义 UICollectionViewCell 类:如何在 Swift 3 中更改高度
【发布时间】:2018-02-16 13:51:56
【问题描述】:

如何在类文件中更改 UICollectionViewCell 的大小?

这是我的代码(或者更确切地说是我的代码与相关功能的 sn-p):

class FeedCell: UICollectionViewCell {

func resizeToText(text: String) {

    self.backgroundColor = UIColor.black

    UIView.animate(withDuration: 0.15, animations: {

        let oldSize = self.feedTextView.frame.size
        var newSize = estimatedFrameForText(text: text, contentViewWidth: oldSize.width, maxSize: 500, font: self.feedTextView.font!, fontSize: self.feedTextView.font!.pointSize)

        newSize.width = oldSize.width

        self.feedTextView.frame.size = newSize

        let heightDiff = oldSize.height - newSize.height



        self.feedImageView.frame.origin.y -= heightDiff

        self.mainView.frame.size.height -= heightDiff

        // I'm changing the cells size but it doesn't change :(
        self.contentView.frame.size.height -= heightDiff


        let oldSize2 = self.feedTextView.frame.size
        let newSize2 = self.feedTextView.contentSize

        self.feedTextView.frame.size = self.feedTextView.contentSize

        let diff = oldSize2.height - newSize2.height

        self.feedImageView.frame.origin.y -= diff

        self.mainView.frame.size.height -= diff

        // here again!
        self.contentView.frame.size.height -= diff

        self.mainView.dropShadowFeed(scale: true)

    })

}

}

我也尝试过 self.frame.size.height 但它也不起作用。

我的目标是使单元格位于提要中并手动调整其显示的文本大小。我如何以编程方式做到这一点?

【问题讨论】:

    标签: swift layout uicollectionview


    【解决方案1】:

    UICollectionViewCell 的框架由下面的委托决定:

    (CGSize)collectionView:(UICollectionView *)collectionView 
                  layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath;
    

    如果你想设置你的collectionviewcell的框架,你需要在这个委托函数中设置它。另一种选择是使用“动态单元格大小”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-18
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多