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

相关文章

深入浅出Spring中的@Autowired自动注入的工作原理及实践应用

《深入浅出Spring中的@Autowired自动注入的工作原理及实践应用》在Spring框架的学习旅程中,@Autowired无疑是一个高频出现却又让初学者头疼的注解,它看似简单,却蕴含着Sprin... 目录深入浅出Spring中的@Autowired:自动注入的奥秘什么是依赖注入?@Autowired

基于Redis自动过期的流处理暂停机制

《基于Redis自动过期的流处理暂停机制》基于Redis自动过期的流处理暂停机制是一种高效、可靠且易于实现的解决方案,防止延时过大的数据影响实时处理自动恢复处理,以避免积压的数据影响实时性,下面就来详... 目录核心思路代码实现1. 初始化Redis连接和键前缀2. 接收数据时检查暂停状态3. 检测到延时过

SpringBoot实现RSA+AES自动接口解密的实战指南

《SpringBoot实现RSA+AES自动接口解密的实战指南》在当今数据泄露频发的网络环境中,接口安全已成为开发者不可忽视的核心议题,RSA+AES混合加密方案因其安全性高、性能优越而被广泛采用,本... 目录一、项目依赖与环境准备1.1 Maven依赖配置1.2 密钥生成与配置二、加密工具类实现2.1

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

MyBatis-Plus 自动赋值实体字段最佳实践指南

《MyBatis-Plus自动赋值实体字段最佳实践指南》MyBatis-Plus通过@TableField注解与填充策略,实现时间戳、用户信息、逻辑删除等字段的自动填充,减少手动赋值,提升开发效率与... 目录1. MyBATis-Plus 自动赋值概述1.1 适用场景1.2 自动填充的原理1.3 填充策略

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.