Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How make a collectionViewCell auto size only by height?

I use feature of autosizing flow layout of collectionView

self.flow = [[UICollectionViewFlowLayout alloc] init];
self.flow.scrollDirection = UICollectionViewScrollDirectionVertical;
self.flow.sectionInset = UIEdgeInsetsZero;
self.flow.estimatedItemSize = CGSizeMake(320, 48);

I want to cells will fill by width full width of CollectionView (widthCell=widthCollectionView)

But i got what autosizing work for whole size. I try playing with Content Compression/Hugging Priority - not effect.

With width of CollectionView equals 320px, i got cell size for label content size.

My cell looks like enter image description here

Constraints Horizontal : H:|-8-[img(48)]-8-[label]-8-|

How to make autosize work only for Vertical but not for Horizontal?

like image 276
Dmitry Nelepov Avatar asked Nov 18 '15 03:11

Dmitry Nelepov


People also ask

How do I make a collection view dynamic height?

Make-CollectionView-Height-Dynamic-According-to-ContentAdd a height constraint to your collection view. Set its priority to 999. Set its constant to any value that only makes it reasonably visible on the storyboard. Change the bottom equal constraint of the collection view to greater or equal.

How do I set the height and width of a CollectionView cell?

Here's a WWDC video demoing this technique. class MyLayout: UICollectionViewFlowLayout { override func prepare() { super. prepare() guard let collectionView = collectionView else { return } itemSize = CGSize(width: ..., height: ...) } }

How do I make my UICollectionView scroll horizontal?

You need to reduce the height of UICollectionView to its cell / item height and select " Horizontal " from the " Scroll Direction " as seen in the screenshot below. Then it will scroll horizontally depending on the numberOfItems you have returned in its datasource implementation.


2 Answers

Actually you are right here is the problem with the constraints priority.

To resolve the priority constraint issue you should update the imageView Leading priority 999 and also label Trailing priority 999 either programmatically or directly from InterFace Builder.

Update priority programmatically by using the KVConstraintExtensionsMaster that I have implemented to update any constraint without IBOutlet reference of Autolayout constraint.

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell1" forIndexPath:indexPath];

    [cell.imageView changeAppliedConstraintPriority:999 forAttribute:NSLayoutAttributeLeading];
    [cell.label changeAppliedConstraintPriority:999 forAttribute:NSLayoutAttributeTrailing];

    return cell;
}

After this Now you need to calculate the only height of the cell

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
     CGFloat width  = CGRectGetWidth(collectionView.bounds);
     CGFloat height  =  /* do here some calculation for height */
     return CGSizeMake(width, height);;
}
like image 45
keshav vishwkarma Avatar answered Oct 26 '22 20:10

keshav vishwkarma


I found a solution.

CollectionViewCell has a delegate preferredLayoutAttributesFittingAttributes

So, this function gives you layoutAttributes sets as estimatedItemSize

You need do next (for Height)

- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes {
    UICollectionViewLayoutAttributes *attr = [super preferredLayoutAttributesFittingAttributes:layoutAttributes]; //Must be called
    CGRect frame = attr.frame;
    frame.size.width = layoutAttributes.size.width; //Key here
    attr.frame = frame;
    return attr;
}

Same you can do to prefer size, width so make constant height (frame.size.height = layoutAttributes.size.height).

If you just copy layoutAttributes and change width, will no effect!

You must call super.

like image 133
Dmitry Nelepov Avatar answered Oct 26 '22 22:10

Dmitry Nelepov