2017-09-07 46 views
0

如何在我的類文件中更改我的UICollectionViewCell的大小?自定義UICollectionViewCell類:如何更改Swift中的高度3

這裏是我的代碼(或者更確切地說,我的代碼與相關函數的代碼段):

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

UICollectionViewCell的框架由以下代表決定:如果你想設置你的collectionviewcell的框架

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

,你需要在這個委託函數來設置。另一種選擇是使用「動態單元大小」。

相關問題