自定义UICollectionViewCell类:如何在Swift 3中更改高度

时间:2017-09-07 18:25:39

标签: swift layout uicollectionview

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

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

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,但它也不起作用。

我的目标是该单元格位于Feed中,并手动调整其显示的文本。我该如何以编程方式执行此操作?

1 个答案:

答案 0 :(得分:2)

UICollectionViewCell的框架由下面的代表决定:

//set the data
localStorage.setItem(key, value);   //syntax example
localStorage.setItem('tokenKey', response.json().token);

//get the data
localStorage.getItem('tokenKey')

//confirm if token is exist or not
return localStorage.getItem('tokenKey') != null;

如果要设置collectionviewcell的框架,则需要在此委托功能中设置它。另一种选择是使用“动态单元大小”。