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
相关推荐
名字不要太长 像我这样就好5 分钟前
【iOS】OC源码阅读——alloc源码分析
笔记·学习·macos·ios·objective-c
zhishishe4 小时前
如何在 iPhone 上恢复已删除的联系人:简短指南
ios·智能手机·iphone
艾小逗5 小时前
uniapp中检查版本,提示升级app,安卓下载apk,ios跳转应用商店
android·ios·uni-app·app升级
zhishishe5 小时前
如何修复卡在恢复模式下的 iPhone:简短指南
windows·macos·ios·objective-c·cocoa·iphone
桃花仙丶5 小时前
iOS/Flutter混合开发之PlatformView配置与使用
flutter·ios·xcode·swift·dart
依旧风轻9 小时前
使用 DoH 查询域名 —— 以 core.tantanapp.com 为例的实战分析
ios·dns·cloudflare·doh·sqi
只可远观10 小时前
Mac搭建Flutter IOS环境详细指南
前端·flutter·macos·ios
一牛10 小时前
译:Swift中的隔离机制介绍
macos·ios·swift
xiaonianzuibang16 小时前
如何修复宝可梦时时刻刻冒险无法正常工作
macos·objective-c·cocoa
画个大饼16 小时前
深度对比:Objective-C与Swift的RunTime机制与底层原理
开发语言·objective-c·swift