简体   繁体   English

根据单元格不改变单元格高度动态地更改UICollectionView高度

[英]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.

相关问题 如何动态更改UICollectionview单元格高度? - How to change UICollectionview Cell Height dynamically? 如何在Swift中动态更改包含UICollectionView单元格的UITableView单元格的高度? - How to dynamically change the height of a UITableView Cell containing a UICollectionView cell in Swift? 如何根据Swift3中的内容自动更改UICollectionView Cell的高度? - How to change the height of UICollectionView Cell automatically according to content in Swift3? 如何在iOS中动态更改UICollectionView单元格高度 - How can change UICollectionView cell height dynamically in iOS 根据UICollectionView中的图像动态更改单元格大小 - Dynamically change cell size according to image in a UICollectionView 动态更改TableView单元格高度 - Dynamically change TableView Cell height 动态更改UITable单元格的高度? - Dynamically change UITable cell height? 动态更新uicollectionview单元格高度 - Dynamically update uicollectionview cell height 根据textView的长度更改自定义单元格的高度 - Change the height of custom cell according to the length of a textView 如何在没有滚动的情况下根据文本长度动态更改textview高度和单元格高度 - how to dynamically change the textview height and cell height according to the text length without the scroll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM