IOS 添加自动布局约束NSLayoutConstraint

自定义UICollectionViewCell,并添加自动布局约束。

objectivec 复制代码
#import "ViewChooseView.h"

#pragma mark - Cell

@interface ViewChooseViewCell : UICollectionViewCell

@property (nonatomic, strong) UILabel *showTemeLab;
@property (nonatomic, strong) UILabel *promotionTextLab;
@property (nonatomic, strong) UIView *showTimeView;
@property (nonatomic, strong) NSDictionary *data;
//马戏场次控件高度
@property (nonatomic, assign) CGFloat showTimeLabHeight;
//促销标签控件高度
@property (nonatomic, assign) CGFloat promotionLabHeight;
//马戏场次顶部边框间距约束
@property (nonatomic, strong) NSLayoutConstraint *showTimeTopConstraint;


@end

@implementation ViewChooseViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self configUI];
    }
    return self;
}

- (void)configUI {
    //计算场次UILabel的高度
    NSString *text = @"";
    self.showTimeLabHeight =[text boundingRectWithSize:CGSizeMake(CLSCREENWIDTH, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size.height;
    //计算促销文案UILabel的高度
    self.promotionLabHeight =[text boundingRectWithSize:CGSizeMake(CLSCREENWIDTH, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:9]} context:nil].size.height;
    
    self.showTimeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 35)];
    self.showTimeView.backgroundColor = [UIColor colorWithHexString:@"F9F9F9" withAlpha:1];
    self.showTimeView.cornerRadius = 5;
    self.showTimeView.layer.borderColor = [UIColor colorWithHexString:@"7890FF" withAlpha:0.5].CGColor;
    self.showTimeView.layer.borderWidth = 0;
    self.showTimeView.clipsToBounds = YES;
    
    self.showTemeLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, self.showTimeLabHeight)];
//使用autolayout 设置为NO ,不是有autolayout 设置为YES;
    self.showTemeLab.translatesAutoresizingMaskIntoConstraints = NO;
    self.showTemeLab.textColor = [UIColor colorWithHexString:@"767676"];
    [self.showTemeLab setFont:[UIFont systemFontOfSize:12]];
    self.promotionTextLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, self.promotionLabHeight)];
    //使用autolayout 设置为NO ,不是有autolayout 设置为YES;
    self.promotionTextLab.translatesAutoresizingMaskIntoConstraints = NO;
    self.promotionTextLab.textColor = [UIColor colorWithHexString:@"FF5B03"];
    [self.promotionTextLab setFont:[UIFont systemFontOfSize:9]];
    [self.showTimeView addSubview:self.showTemeLab];
    [self.showTimeView addSubview:self.promotionTextLab];
    //马戏场次顶部约束间距,需要动态改变
    self.showTimeTopConstraint = [self.showTemeLab.topAnchor constraintEqualToAnchor:self.showTimeView.topAnchor constant:(35-self.showTimeLabHeight)/2];
    //添加自动布局约束
    [NSLayoutConstraint activateConstraints:@[
        [self.promotionTextLab.centerXAnchor constraintEqualToAnchor:self.showTimeView.centerXAnchor],
        [self.showTemeLab.centerXAnchor constraintEqualToAnchor:self.showTimeView.centerXAnchor],
        //[self.showTemeLab.centerYAnchor constraintEqualToAnchor:self.showTimeView.centerYAnchor],
        //[self.promotionTextLab.centerYAnchor constraintEqualToAnchor:self.showTimeView.centerYAnchor],
        self.showTimeTopConstraint,
        [self.promotionTextLab.topAnchor constraintEqualToAnchor:self.showTemeLab.bottomAnchor constant:0],
    ]];

    [self.contentView addSubview:self.showTimeView];
    
}
- (void)setData:(NSDictionary *)data{
    _data = data;
    CGFloat showTimeWidth = [[data objectForKey:@"width"] floatValue];
    CGFloat promotionTextWidth = [[data objectForKey:@"promotionTextWidth"] floatValue];
    self.showTimeView.width = showTimeWidth>promotionTextWidth?showTimeWidth:promotionTextWidth;
    self.showTemeLab.width = showTimeWidth;
    self.promotionTextLab.width = promotionTextWidth;
    
    NSString *title = [data safeStringForKey:@"title"];
    NSString *promotionText = [data safeStringForKey:@"promotionText"];
    BOOL isSelect = [[data objectForKey:@"isSelect"] boolValue];
    BOOL isEnable = [[data objectForKey:@"isEnable"] boolValue];
    self.showTemeLab.text = title;
    
    self.promotionTextLab.text = promotionText;
    if([promotionText clIsNotEmpty]){
        self.promotionTextLab.hidden = NO;
        //改变顶部约束间距
        self.showTimeTopConstraint.constant = (35-self.showTimeLabHeight-self.promotionLabHeight)/2;
    }else{
        self.promotionTextLab.hidden = YES;
        //改变顶部约束间距
        self.showTimeTopConstraint.constant = (35-self.showTimeLabHeight)/2;
    }

    self.promotionTextLab.textColor = [UIColor colorWithHexString:@"#FF5B03"];
    
    if(isEnable){
        
        if(isSelect){
            self.showTimeView.layer.borderWidth = 0.5;
            self.showTimeView.backgroundColor = [UIColor colorWithHexString:@"7890FF" withAlpha:0.02];
            self.showTemeLab.textColor = [UIColor colorWithHexString:@"#7809FF"];
        }else{
            self.showTimeView.layer.borderWidth = 0;
            self.showTimeView.backgroundColor = [UIColor colorWithHexString:@"F9F9F9" withAlpha:1];
            self.showTemeLab.textColor = [UIColor colorWithHexString:@"#767676"];
        }
    }else{
        self.showTimeView.layer.borderWidth = 0;
        self.showTimeView.backgroundColor = [UIColor colorWithHexString:@"F9F9F9" withAlpha:1];
        self.showTemeLab.textColor = [UIColor colorWithHexString:@"#767676" withAlpha:0.6];
    }

}

@end
相关推荐
貂蝉空大1 小时前
uni-app开发安卓和ios app 真机调试
android·ios·uni-app
胖虎12 小时前
iOS 中的圆角与平滑圆角:从新特性到老项目适配
ios·圆角·平滑圆角·cornercurve
志飞2 小时前
ios UICollectionView使用自定义UICollectionViewCell
ios·collectionview·自定义cell
闲猫7 小时前
go WEB框架
前端·golang·web·xcode
Neo Evolution8 小时前
Flutter与移动开发的未来:谷歌的技术愿景与实现路径
android·人工智能·学习·ios·前端框架·webview·着色器
B.-11 小时前
在 Flutter 中实现文件读写
开发语言·学习·flutter·android studio·xcode
哈里哈气1 天前
某手sig3-ios算法 Chomper黑盒调用
objective-c·ios逆向·frida·chomper
没头脑的ht1 天前
ios App的启动过程和启动优化
ios
敲代码的鱼哇1 天前
设备唯一ID获取,支持安卓/iOS/鸿蒙Next(uni-device-id)UTS插件
android·ios·uniapp·harmonyos
江上清风山间明月1 天前
Flutter最简单的路由管理方式Navigator
android·flutter·ios·路由·页面管理·navigator