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 小时前
CALayer的异步处理
macos·ios·cocoa
吴Wu涛涛涛涛涛Tao2 小时前
一步到位:用 Very Good CLI × Bloc × go_router 打好 Flutter 工程地基
flutter·ios
@PHARAOH3 小时前
WHAT - 依赖管理工具 CocoaPods
xcode·cocoapods
杂雾无尘4 小时前
开发者必看:如何在 iOS 应用中完美实现动态自定义字体!
ios·swift·apple
kymjs张涛6 小时前
零一开源|前沿技术周报 #6
前端·ios·harmonyos
与火星的孩子对话1 天前
Unity进阶课程【六】Android、ios、Pad 终端设备打包局域网IP调试、USB调试、性能检测、控制台打印日志等、C#
android·unity·ios·c#·ip
小溪彼岸1 天前
【XCode】Copilot for XCode AI编程助手
xcode
恋猫de小郭2 天前
Flutter Widget Preview 功能已合并到 master,提前在体验毛坯的预览支持
android·flutter·ios
点金石游戏出海2 天前
每周资讯 | Krafton斥资750亿日元收购日本动画公司ADK;《崩坏:星穹铁道》新版本首日登顶iOS畅销榜
游戏·ios·业界资讯·apple·崩坏星穹铁道
旷世奇才李先生2 天前
Swift 安装使用教程
开发语言·ios·swift