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
相关推荐
软件聚导航39 分钟前
uniapp 实现 ble蓝牙同时连接多台蓝牙设备,支持app、苹果(ios)和安卓手机,以及ios连接蓝牙后的一些坑
android·ios·uni-app
q567315238 小时前
用 PHP或Python加密字符串,用iOS解密
java·python·ios·缓存·php·命令模式
Hgc558886668 小时前
iOS 18.2 重磅更新:6个大动作
ios
️ 邪神8 小时前
【Android、IOS、Flutter、鸿蒙、ReactNative 】标题栏
android·flutter·ios·鸿蒙·reatnative
lrlianmengba9 小时前
推荐一款好用的ios传输设备管理工具:AnyTrans for iOS
macos·ios·cocoa
XZHOUMIN11 小时前
网易博客旧文-----XCODE离线帮助文件使用的补充说明
ide·macos·xcode
2401_8524035514 小时前
整理iPhone空间:iphone怎么删除相簿
ios·iphone
Jewel10516 小时前
Flutter代码混淆
android·flutter·ios
HelloMagina16 小时前
Xcode控制台“po“错误:表达式解析失败
ide·macos·xcode
安和昂19 小时前
【iOS】知乎日报第三周总结
ios