如何使一个collectionViewCell汽车大小仅受高度? [英] How make a collectionViewCell auto size only by height?

查看:362
本文介绍了如何使一个collectionViewCell汽车大小仅受高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用自动调整大小的CollectionView的流布局的特点

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

我想细胞将的CollectionView的宽度宽填写(widthCell = widthCollectionView)

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

但我得到了自动调整大小工作,为全尺寸。我尝试用内容的COM pression /抱死优先打 - 不影响

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

使用的CollectionView的宽度等于320像素,我得到了单元尺寸为标签内容的大小。

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

我的手机看起来像

约束水平:H:| -8- [IMG(48)] - 8- [标签] -8- |

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

如何使自动调整工作只为垂直而不是水平?

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

推荐答案

我找到了解决办法。

Col​​lectionViewCell有一个委托<一个href=\"https://developer.apple.com/library/$p$prelease/ios/documentation/UIKit/Reference/UICollectionReusableView_class/index.html\">$p$pferredLayoutAttributesFittingAttributes

CollectionViewCell has a delegate preferredLayoutAttributesFittingAttributes

所以,这个功能让ülayoutAttributes集作为<一个href=\"https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewFlowLayout_class/\">estimatedItemSize

So, this function gives u 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;
}

同样ü可以做preFER大小,宽度,以便使恒定的高度(frame.size.height = layoutAttributes.size.height)。

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

如果你刚刚复制layoutAttributes及变化幅度,将没有效果!

ü必须调用超。

这篇关于如何使一个collectionViewCell汽车大小仅受高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆