IOS 添加自动布局约束NSLayoutConstraint

server/2024/12/22 9:57:44/

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

#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


http://www.ppmy.cn/server/23966.html

相关文章

Java ImageIo 报错 Unsupported image type

说明 在上传CMYK颜色类型的jpg图片时&#xff0c;Java的ImageIo会报错Unsupported image type 解决方法 只需要引入依赖&#xff0c;不需要修改代码&#xff0c;ImageIo会自动发现扩展 <dependency><groupId>com.twelvemonkeys.imageio</groupId><arti…

【Qt】无法创建或打开CMake项目

无法创建或打开CMake项目 没有CMake选项 原因 卸载重装了qt&#xff0c;安装时勾选了cmake&#xff0c;发现还是一样没有。。。 后面发现是插件里没有勾选上CMake。。。很无语 不是应该默认就有的吗 不懂了 解决 插件勾选重启就好了

简要说说软分叉和硬分叉。

前言 一、软分叉 二、硬分叉 三、用途 总结 前言 软分叉和硬分叉是区块链技术中的两个重要概念&#xff0c;它们通常与加密货币的网络升级有关。下面我将分别解释这两个概念&#xff0c;并提供一些例子来帮助理解。下面是方便理解软分叉和硬分叉的图 一、软分叉 软分叉是一…

如何利用 GPT 自我提高写作能力

GPT革命&#xff1a;如何用AI技术重新定义写作 介绍 在我们的数字时代&#xff0c;了解自我提高写作的必要性至关重要。 随着 GPT 的兴起&#xff0c;我们正在见证书写的变革时代。 这篇扩展文章深入探讨了 GPT 如何显着提高写作技能。 拥抱未来&#xff1a; 人工智能时代的写…

JAVASE 数组相关知识

递归 A方法调用B方法&#xff0c;我们很容易理解 递归就是:A方法调用A方法&#xff0c;即自己调用自己 利用递归可以用简单的程序来解决一些复杂的问题。它通常把一个大型复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解&#xff0c;递归策略只需少量的程序就可…

linux下建立cpp文件,然后通过cmake编译。

main.cpp #include<iostream> using namespace std;int main(){cout<<"hello world"<<endl; return 0; }相同目录下建立 CMakeLists.txt PROJECT (HELLO) SET(SRC_LIST main.cpp) ADD_EXECUTABLE(hello ${SRC_LIST})[rootlocalhost cmake01…

sklearn混淆矩阵的计算和seaborn可视化

为了计算语义分割的指标miou&#xff0c;需要生成的中间过程就是混淆矩阵。 iou intersection / union 每个类别的平均iou就是mean iou。 使用sklearn自带的confusion_matrix能很容易生成混淆矩阵&#xff0c;可以进行混淆矩阵的可视化观察哪个类别分割的不好。 from skle…

前端JS算法-冒泡排序

原理&#xff1a;相邻的数据进行两两比较&#xff0c;小数放在前面&#xff0c;大数放在后面&#xff0c;这样一趟下来&#xff0c;最小的数就被排在了第一位&#xff0c;第二趟也是如此&#xff0c;如此类推&#xff0c;直到所有的数据排序完成。 描述&#xff1a; <1&g…