[英]Change UICollectionView Height Dynamically according to cell's not change cell height
I am developing IOS App.Click to Tableview cell Than value show on collectionViewCell but I want change collectionView height according to cell's. 我正在开发IOS App。单击到Tableview单元格而不是collectionViewCell上的值显示,但是我想根据单元格的值更改collectionView的高度。
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return CGSizeMake(_collectionView.frame.size.width , _collectionView.frame.size.height+30);
}
you can change it by using contentSize of its scrollView after the collectionview is loaded.. if you update any cell insert items or delete items you need to update height again ie 您可以通过在加载collectionview后使用它的scrollView的contentSize来更改它。如果更新任何单元格插入项或删除项,则需要再次更新高度,即
myCollectionView.reload()
now updating frame or height 现在更新框架或高度
//if you are using auto-resizing you can update frame
let frame = myCollectionView.frame
frame.height = myCollectionView.contentSize.height
myCollectionView.frame = frame
and if you are using autolayout make and iboutlet for your collectionview's height constraint 并且如果您使用autolayout make和iboutlet作为collectionview的高度约束
@IBOutlet collectionViewHeightConstraint:NSLayoutContraint!
now update this contraint 现在更新此约束
collectionViewHeightConstraint.constant = myCollectionView.contentSize.height
You need to add this code 您需要添加此代码
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
var numberOfCellInRow : Int = yourArray.count
var padding : Int = 5
var collectionCellWidth : CGFloat = (self.view.frame.size.width/CGFloat(numberOfCellInRow)) - CGFloat(padding)
return CGSize(width: collectionCellWidth , height: collectionCellWidth)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.