IOS Masonry自动布局

2024-02-28 15:18
文章标签 布局 自动 ios masonry

本文主要是介绍IOS Masonry自动布局,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

之前项目用Frame布局,这个项目登录用了VFL,后来觉得用Masonry,前天布局TableViewCell时用了下 ,觉得还不错。

#import "Masonry.h"
#import "MASViewAttribute.h"

先看效果图:

#import "ReportsCell.h"
//#import "Masonry.h"
#import "YZPUIFormatMacros.h"
#import "Global.h"static const CGFloat kIconWidth                 = 55.0f;
static const CGFloat kIconHeight                = 55.0f;@implementation ReportsCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];if (self) {self.backgroundColor = [UIColor whiteColor];UIView *headerView = [[UIView alloc]init];headerView.backgroundColor = YZPColorDividingLineOutdide;[self addSubview:headerView];[headerView mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(@0);make.top.equalTo(@0);make.width.equalTo(@(MainWidth));make.height.equalTo(@(YZPSpaceHSmall));}];UIView *topLine=[self lineView];[headerView addSubview:topLine];[topLine mas_makeConstraints:^(MASConstraintMaker *make) {make.top.equalTo(headerView.top);make.left.equalTo(headerView.left);make.width.equalTo(headerView.width);make.height.equalTo(@0.5);}];UIView *bottomLine=[self lineView];[headerView addSubview:bottomLine];[bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {make.top.equalTo(headerView.bottom).with.offset(-0.5);make.left.equalTo(headerView.left);make.width.equalTo(headerView.width);make.height.equalTo(@0.5);}];_orderIdLabel=[[UILabel alloc]init];_orderIdLabel.font = YZPFontSubhead;_orderIdLabel.textColor = YZPColorTextSubhead;[self addSubview:_orderIdLabel];[_orderIdLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.top.equalTo(headerView.bottom).with.offset(15);make.left.equalTo(@10);make.width.equalTo(headerView.width);make.height.equalTo(@20);}];_orderIconView=[[UIImageView alloc]init];[self addSubview:_orderIconView];[_orderIconView makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(@15);make.top.equalTo(_orderIdLabel.bottom).with.offset(15);make.width.equalTo(@(kIconWidth));make.height.equalTo(@(kIconHeight));}];_orderTitleLabel=[[UILabel alloc]init];_orderTitleLabel.font=YZPFontSubhead;_orderTitleLabel.textColor=[UIColor colorWithRed:0.141f green:0.141f blue:0.141f alpha:1.00f];_orderTitleLabel.numberOfLines=2;[self addSubview:_orderTitleLabel];[_orderTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(_orderIconView.right).with.offset(15);make.top.equalTo(_orderIconView.top).with.offset(2);make.width.equalTo(@(MainWidth-170));make.height.lessThanOrEqualTo(@(35));}];_orderSpecLabel=[[UILabel alloc]init];_orderSpecLabel.font=YZPFontExplain;_orderSpecLabel.textColor=[UIColor colorWithRed:0.400f green:0.400f blue:0.400f alpha:1.00f];[self addSubview:_orderSpecLabel];[_orderSpecLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(_orderTitleLabel.left);make.top.equalTo(_orderTitleLabel.bottom).offset(10);make.width.equalTo(@(MainWidth-135));make.height.lessThanOrEqualTo(@(20));}];_productsCountLabel=[[UILabel alloc]init];_productsCountLabel.font=YZPFontContent;_productsCountLabel.textAlignment=NSTextAlignmentRight;_productsCountLabel.textColor=[UIColor colorWithRed:0.400f green:0.400f blue:0.400f alpha:1.00f];[self addSubview:_productsCountLabel];[_productsCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.top.equalTo(_orderTitleLabel.top);make.left.equalTo(_orderTitleLabel.right).offset(5);make.right.equalTo(self.right).with.offset(-15);make.height.equalTo(_orderTitleLabel.height);}];UIView *centerLine=[self lineView];[self addSubview:centerLine];[centerLine mas_makeConstraints:^(MASConstraintMaker *make) {make.top.equalTo(_orderIconView.bottom).offset(15);make.left.equalTo(self);make.width.equalTo(self);make.height.equalTo(@0.5);}];_userNameLabel=[[UILabel alloc]init];_userNameLabel.font=YZPFontExplain;_userNameLabel.textColor=YZPColorTextExplain;
//        _userNameLabel.numberOfLines=2;_userNameLabel.textAlignment=NSTextAlignmentLeft;[self addSubview:_userNameLabel];[_userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.left.equalTo(self.left).with.offset(15);make.top.equalTo(centerLine.bottom).offset(10);make.width.lessThanOrEqualTo(@150);make.height.lessThanOrEqualTo(@35);}];_userPhoneLabel=[[UILabel alloc]init];_userPhoneLabel.font=YZPFontExplain;_userPhoneLabel.textColor=YZPColorTextExplain;_userPhoneLabel.textAlignment=NSTextAlignmentRight;[self addSubview:_userPhoneLabel];[_userPhoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.top.equalTo(_userNameLabel.top);make.right.equalTo(self.right).with.offset(-15);make.height.lessThanOrEqualTo(@35);make.width.lessThanOrEqualTo(@150);}];_userAddressLabel=[[UILabel alloc]init];_userAddressLabel.font=YZPFontExplain;_userAddressLabel.textColor=YZPColorTextExplain;[self addSubview:_userAddressLabel];[_userAddressLabel mas_makeConstraints:^(MASConstraintMaker *make) {make.top.equalTo(_userNameLabel.bottom).with.offset(10);make.left.equalTo(self.left).with.offset(15);
//            make.bottom.equalTo(self.bottom).with.offset(-10);make.width.equalTo(self.width);make.height.lessThanOrEqualTo(@35);}];}return self;
}
-(UIView *)lineView
{UIView *line=[[UIView alloc]init];
//    line.backgroundColor=[UIColor redColor];line.backgroundColor=[UIColor colorWithRed:0.800f green:0.800f blue:0.800f alpha:1.00f];return line;
}- (void)awakeFromNib {// Initialization code
}- (void)setSelected:(BOOL)selected animated:(BOOL)animated {[super setSelected:selected animated:animated];// Configure the view for the selected state
}@end


这篇关于IOS Masonry自动布局的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/755868

相关文章

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机

HTML5实现的移动端购物车自动结算功能示例代码

《HTML5实现的移动端购物车自动结算功能示例代码》本文介绍HTML5实现移动端购物车自动结算,通过WebStorage、事件监听、DOM操作等技术,确保实时更新与数据同步,优化性能及无障碍性,提升用... 目录1. 移动端购物车自动结算概述2. 数据存储与状态保存机制2.1 浏览器端的数据存储方式2.1.

一文详解MySQL如何设置自动备份任务

《一文详解MySQL如何设置自动备份任务》设置自动备份任务可以确保你的数据库定期备份,防止数据丢失,下面我们就来详细介绍一下如何使用Bash脚本和Cron任务在Linux系统上设置MySQL数据库的自... 目录1. 编写备份脚本1.1 创建并编辑备份脚本1.2 给予脚本执行权限2. 设置 Cron 任务2

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

Python使用pynput模拟实现键盘自动输入工具

《Python使用pynput模拟实现键盘自动输入工具》在日常办公和软件开发中,我们经常需要处理大量重复的文本输入工作,所以本文就来和大家介绍一款使用Python的PyQt5库结合pynput键盘控制... 目录概述:当自动化遇上可视化功能全景图核心功能矩阵技术栈深度效果展示使用教程四步操作指南核心代码解析

SpringBoot实现文件记录日志及日志文件自动归档和压缩

《SpringBoot实现文件记录日志及日志文件自动归档和压缩》Logback是Java日志框架,通过Logger收集日志并经Appender输出至控制台、文件等,SpringBoot配置logbac... 目录1、什么是Logback2、SpringBoot实现文件记录日志,日志文件自动归档和压缩2.1、

SpringCloud使用Nacos 配置中心实现配置自动刷新功能使用

《SpringCloud使用Nacos配置中心实现配置自动刷新功能使用》SpringCloud项目中使用Nacos作为配置中心可以方便开发及运维人员随时查看配置信息,及配置共享,并且Nacos支持配... 目录前言一、Nacos中集中配置方式?二、使用步骤1.使用$Value 注解2.使用@Configur

Golang实现Redis分布式锁(Lua脚本+可重入+自动续期)

《Golang实现Redis分布式锁(Lua脚本+可重入+自动续期)》本文主要介绍了Golang分布式锁实现,采用Redis+Lua脚本确保原子性,持可重入和自动续期,用于防止超卖及重复下单,具有一定... 目录1 概念应用场景分布式锁必备特性2 思路分析宕机与过期防止误删keyLua保证原子性可重入锁自动