UICollectionViewCell內(nèi)的操作
1.撐起cell的lable自適應(yīng)
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
// 用約束來(lái)初始化控件:
self.textLabel = [[UILabel alloc] init];
self.textLabel.textAlignment =NSTextAlignmentCenter;
[self.contentView addSubview:self.textLabel];
[self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
// make 代表約束:
make.top.equalTo(self.contentView).with.offset(0); // 對(duì)當(dāng)前view的top進(jìn)行約束,距離參照view的上邊界是 :
make.left.equalTo(self.contentView).with.offset(0); // 對(duì)當(dāng)前view的left進(jìn)行約束,距離參照view的左邊界是 :
make.height.equalTo(@(itemHeight)); // 高度
make.right.equalTo(self.contentView).with.offset(0); // 對(duì)當(dāng)前view的right進(jìn)行約束,距離參照view的右邊界是 :
}];
}
return self;
}
2.要重寫(xiě)UICollectionViewLayoutAttributes方法
- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
UICollectionViewLayoutAttributes *attributes = [super preferredLayoutAttributesFittingAttributes:layoutAttributes];
//32是cell與父視圖左右邊距
CGRect rect = [self.textLabel.text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-32, itemHeight) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:13]} context:nil];
rect.size.width+=24;//lable與cell左右的間距
rect.size.height+=14;//lable與cell上下的間距
attributes.frame = rect;
return attributes;
}
UICollectionView里的操作
1.創(chuàng)建一個(gè)UICollectionViewFlowLayout的子類(lèi)重寫(xiě)父類(lèi)的方法- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSMutableArray * attributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
// 設(shè)置的最大間距饱普,一般這個(gè)間距是固定的运挫,就用外部能寫(xiě)的最小間距值
CGFloat maximumSpacing = self.minimumInteritemSpacing;
if (attributes.count > 0) {
UICollectionViewLayoutAttributes *firstAttributes = attributes[0];
CGRect frame = firstAttributes.frame;
frame.origin.x = self.sectionInset.left;
firstAttributes.frame = frame;
}
//第一個(gè)frame不用更改 從第二個(gè)循環(huán)到最后一個(gè)
for (NSInteger i = 1 ; i < attributes.count ; i++ ) {
// 當(dāng)前的attribute
UICollectionViewLayoutAttributes * currentLayoutAttributes = attributes[i];
// 上一個(gè)attribute
UICollectionViewLayoutAttributes * prevLayoutAttributes = attributes[i - 1];
// 前一個(gè)cell的最右邊
CGFloat origin = CGRectGetMaxX(prevLayoutAttributes.frame);
// 如果當(dāng)前一個(gè)cell的最右邊加上我們的想要的間距加上當(dāng)前cell的寬度依然在contentSize中,我們改變當(dāng)前cell的原點(diǎn)位置
// 不加這個(gè)判斷的后果是套耕,UICollectionView只顯示一行谁帕,原因是下面所有的cell的x值都被加到第一行最后一個(gè)元素的后面了 考慮cell與view的左右間距
if (origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width -self.sectionInset.right ) {
CGRect frame = currentLayoutAttributes.frame;
frame.origin.x = origin + maximumSpacing;
currentLayoutAttributes.frame = frame;
} else {
CGRect frame = currentLayoutAttributes.frame;
frame.origin.x = self.sectionInset.left;
currentLayoutAttributes.frame = frame;
}
}
return attributes;
}
estimatedItemSize設(shè)置
layout.estimatedItemSize = CGSizeMake(20, 32);