UICollectionView 實(shí)現(xiàn)等間距自適應(yīng)寬度cell

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);
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市冯袍,隨后出現(xiàn)的幾起案子雇卷,更是在濱河造成了極大的恐慌,老刑警劉巖颠猴,帶你破解...
    沈念sama閱讀 219,188評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件峻仇,死亡現(xiàn)場(chǎng)離奇詭異儡首,居然都是意外死亡援岩,警方通過(guò)查閱死者的電腦和手機(jī)遍坟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)资盅,“玉大人调榄,你說(shuō)我怎么就攤上這事『强福” “怎么了每庆?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,562評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)今穿。 經(jīng)常有香客問(wèn)我缤灵,道長(zhǎng),這世上最難降的妖魔是什么蓝晒? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,893評(píng)論 1 295
  • 正文 為了忘掉前任腮出,我火速辦了婚禮,結(jié)果婚禮上芝薇,老公的妹妹穿的比我還像新娘胚嘲。我一直安慰自己,他們只是感情好洛二,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布馋劈。 她就那樣靜靜地躺著,像睡著了一般晾嘶。 火紅的嫁衣襯著肌膚如雪妓雾。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,708評(píng)論 1 305
  • 那天变擒,我揣著相機(jī)與錄音君珠,去河邊找鬼寝志。 笑死娇斑,一個(gè)胖子當(dāng)著我的面吹牛策添,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播毫缆,決...
    沈念sama閱讀 40,430評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼唯竹,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了苦丁?” 一聲冷哼從身側(cè)響起浸颓,我...
    開(kāi)封第一講書(shū)人閱讀 39,342評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎旺拉,沒(méi)想到半個(gè)月后产上,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,801評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蛾狗,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評(píng)論 3 337
  • 正文 我和宋清朗相戀三年晋涣,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片沉桌。...
    茶點(diǎn)故事閱讀 40,115評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡谢鹊,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出留凭,到底是詐尸還是另有隱情佃扼,我是刑警寧澤,帶...
    沈念sama閱讀 35,804評(píng)論 5 346
  • 正文 年R本政府宣布蔼夜,位于F島的核電站兼耀,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏求冷。R本人自食惡果不足惜翠订,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望遵倦。 院中可真熱鬧尽超,春花似錦、人聲如沸梧躺。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,008評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)掠哥。三九已至巩踏,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間续搀,已是汗流浹背塞琼。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,135評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留禁舷,地道東北人彪杉。 一個(gè)月前我還...
    沈念sama閱讀 48,365評(píng)論 3 373
  • 正文 我出身青樓毅往,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親派近。 傳聞我的和親對(duì)象是個(gè)殘疾皇子攀唯,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容