ios . -- UICollectionView --cell 自適應

來源:互聯網
上載者:User

標籤:lis   inter   txt   property   end   tco   rect   使用   price   

#pragma mark — 視圖控制器中使用:(關鍵)    layout.estimatedItemSize = CGSizeMake(WIDTH, 60);  // layout約束這邊必須要用estimatedItemSize才能實現自適應,使用itemSzie無效
//// 商品詳情 容器 詳情 cell#import <UIKit/UIKit.h>@interface DetailsViewCell : UICollectionViewCell@property (nonatomic,strong) CategorizeListOfGoodsModel *goodsitemmodel;@end
#import "DetailsViewCell.h"@interface DetailsViewCell()@property (nonatomic,strong) UILabel *titletxt;//標題@property (nonatomic,strong) UILabel *pracetxt;//價格@property (nonatomic,strong) UILabel *msaletxt;//銷量@end@implementation DetailsViewCell- (void)setGoodsitemmodel:(CategorizeListOfGoodsModel *)goodsitemmodel{    _goodsitemmodel = goodsitemmodel;    self.titletxt.text = [NSString stringWithFormat:@"%@",goodsitemmodel.title];    self.pracetxt.text = [NSString stringWithFormat:@"¥%@",goodsitemmodel.voucher_price];    self.msaletxt.text = [NSString stringWithFormat:@"月銷%@",goodsitemmodel.m_sale];            [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {        make.left.top.mas_equalTo(0);        make.width.mas_equalTo([UIScreen mainScreen].bounds.size.width);        make.bottom.mas_equalTo(self.pracetxt.mas_bottom).offset(10);    }];            [self.titletxt mas_makeConstraints:^(MASConstraintMaker *make) {        make.top.equalTo(self).mas_offset(10);        make.left.equalTo(self).mas_offset(10);        make.right.equalTo(self).mas_offset(-10);    }];        [self.pracetxt mas_makeConstraints:^(MASConstraintMaker *make) {        make.top.equalTo(self.titletxt.mas_bottom).mas_offset(10);        make.bottom.equalTo(self).mas_offset(-10);        make.left.equalTo(self).mas_offset(10);    }];    [self.msaletxt mas_makeConstraints:^(MASConstraintMaker *make) {        make.top.equalTo(self.titletxt.mas_bottom).mas_offset(10);        make.bottom.equalTo(self).mas_offset(-10);        make.right.equalTo(self).mas_offset(-10);    }];}- (instancetype)initWithFrame:(CGRect)frame{    if (self = [super initWithFrame:frame]) {        [self initWithUIFrame:frame];    }    return self;}- (void)initWithUIFrame:(CGRect)rect{    self.contentView.backgroundColor = [UIColor whiteColor];    self.titletxt = [[UILabel alloc]init];    self.titletxt.font = [UIFont systemFontOfSize:14];    self.titletxt.textColor = [YColor YColorWithHexString:@"#333333"];    self.titletxt.numberOfLines = 0;    [self.contentView addSubview:self.titletxt];        self.pracetxt = [[UILabel alloc]init];    self.pracetxt.font = [UIFont systemFontOfSize:16];    self.pracetxt.textColor = [YColor YColorWithHexString:@"#F32F19"];    [self.contentView addSubview:self.pracetxt];        self.msaletxt = [[UILabel alloc]init];    self.msaletxt.font = [UIFont systemFontOfSize:12];    self.msaletxt.textColor = [YColor YColorWithHexString:@"#999999"];    [self.contentView addSubview:self.msaletxt];        }#pragma mark — 實現自適應文字寬度的關鍵步驟:item的layoutAttributes- (UICollectionViewLayoutAttributes*)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes*)layoutAttributes {    [self setNeedsLayout];    [self layoutIfNeeded];    CGSize size = [self.contentView systemLayoutSizeFittingSize: layoutAttributes.size];    CGRect cellFrame = layoutAttributes.frame;    cellFrame.size.height= size.height;    layoutAttributes.frame= cellFrame;    return layoutAttributes;}@end

 

ios . -- UICollectionView --cell 自適應

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.