【问题标题】:Change UICollectionView Height Dynamically according to cell's not change cell height根据单元格的不更改单元格高度动态更改 UICollectionView 高度
【发布时间】:2017-10-10 04:47:29
【问题描述】:

我正在开发 IOS App.Click to Tableview cell than value show on collectionViewCell 但我想根据单元格的值更改 collectionView 的高度。

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(_collectionView.frame.size.width , _collectionView.frame.size.height+30);

}

【问题讨论】:

  • 您想更改集合视图高度或单元格高度??
  • 你想根据单元格数改变collectionView的高度吗?
  • 是根据单元格数改变collectionview高度

标签: ios objective-c uicollectionview


【解决方案1】:

你可以使用它的 scrollView 的 contentSize 来改变它 加载collectionview后..如果您更新任何单元格插入项目或删除项目,您需要再次更新高度 即

myCollectionView.reload()

现在更新框架或高度

//if you are using auto-resizing you can update frame
let frame = myCollectionView.frame
frame.height = myCollectionView.contentSize.height
myCollectionView.frame = frame

如果你使用 autolayout make 和 iboutlet 作为你的 collectionview 的高度约束

@IBOutlet collectionViewHeightConstraint:NSLayoutContraint!

现在更新这个约束

collectionViewHeightConstraint.constant = myCollectionView.contentSize.height

【讨论】:

    【解决方案2】:

    您需要添加此代码

    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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多