Different cell size in UICollectionview

后端 未结 4 2181
梦谈多话
梦谈多话 2020-12-18 18:10

In my iphone app,I am showing images on a collection view. I fetch the images from urls,with urls I get the image size also. eg:- let\'s say there are two kinds of images la

4条回答
  •  醉梦人生
    2020-12-18 18:48

    1) You could maintain and swap out multiple layout objects, but there's a simpler way. Just add the following to your UICollectionViewController subclass and adjust the sizes as required:

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
    {    
        // Adjust cell size for orientation
        if (UIDeviceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
            return CGSizeMake(170.f, 170.f);
        }
        return CGSizeMake(192.f, 192.f);
    }
    
    - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
    {
        [self.collectionView performBatchUpdates:nil completion:nil];
    }
    

    Calling -performBatchUpdates:completion: will invalidate the layout and resize the cells with animation (you can just pass nil to both block params if you've no extra adjustments to perform).

    2) Instead of hardcoding the item sizes in -collectionView:layout:sizeForItemAtIndexPath, just divide the height or width of the collectionView's bounds by the number of cells you want to fit on screen. Use the height if your UICollectionView scrolls horizontally or the width if it scrolls vertically.

提交回复
热议问题