gpt4 book ai didi

swift - 自定义 UICollectionViewCell 类 : how to change height in Swift 3

转载 作者:行者123 更新时间:2023-11-28 12:17:04 25 4
gpt4 key购买 nike

如何在类文件中更改 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 但它也不起作用。

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

最佳答案

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

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

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

关于swift - 自定义 UICollectionViewCell 类 : how to change height in Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46102884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com