UICollectionViewCell dynamic height with autolayout

前端 未结 3 999
隐瞒了意图╮
隐瞒了意图╮ 2020-12-10 14:54

I am using auto layout with UICollectionViewCell. So the idea is to allow CollectionViewCell to determine it\'s size based on layouts. All the constraints are set properly b

相关标签:
3条回答
  • 2020-12-10 15:03

    I think you should return this size:

    return [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    
    0 讨论(0)
  • 2020-12-10 15:21

    here is the solution

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {
        YourCollectionCell * cell = (YourCollectionCell *) [YourCollectionViewObj cellForItemAtIndexPath:indexPath];
    
                if (cell == nil) {
    
                    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"YourCollectionCell" owner:self options:nil];
                    cell = [topLevelObjects objectAtIndex:0];
                    cell.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 20);
                    // SET YOUR CONTENT
                    [cell layoutIfNeeded];
    
                }
    
                CGSize CellSize = [cell systemLayoutSizeFittingSize:UILayoutFittingCompressedSize withHorizontalFittingPriority:UILayoutPriorityDefaultHigh verticalFittingPriority:UILayoutPriorityDefaultLow];
    
    
                return CellSize;
     }
    
    0 讨论(0)
  • 2020-12-10 15:28

    For your dynamic height of the UICollectionViewCell in UIcollectionViewcontroller , you need to implement below method: -

    (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    
    0 讨论(0)
提交回复
热议问题