NSLayoutConstraint 动态布局

使用NSLayoutConstraint添加约束线 参考链接: http://www.jianshu.com/p/a4812b01a2a3 //view1可以待布局元件也可以为参考元件 //view2可以参考元件也可以为待布局元件 //事件函数计算公式: //view1.attr1 <relation> view2.attr2 * multiplier + constant //view1:约束线左(上)边端的元件(左(上)元件1) //attr1:指定view1约束线相关的轴线属性attr1(左(上)元 阅读详情

    在iOS开发中,由于屏幕是变化的,所以有时候 布局是动态的,所以本期带来的就是NSLayoutConstraint动态布局。

    在用NSLayoutConstraint布局之前translatesAutoresizingMaskIntoConstraints属性一定要设为NO。

  先介绍一下用的一个方法。

+(instancetype)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(nullable id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;

参数view1:目标视图。

参数attr1:目标视图所要设置的属性。

参数relation:目标视图属性与参照视图的关系 有三种

    NSLayoutRelationLessThanOrEqual = -1,小于等于

    NSLayoutRelationEqual = 0,等于

    NSLayoutRelationGreaterThanOrEqual = 1,大于等于

参数view2:参照视图。

参数attr2:参照视图属性。

参数multiplier:乘数--目标视图与参照视图的属性的倍数关系。不可为0

参数c:常数--目标视图与参照视图的属性的加减关系。

属性NSLayoutAttribute:

    NSLayoutAttributeLeft       对象左边距

    NSLayoutAttributeRight,     对象右边距

    NSLayoutAttributeTop, 对象上边距

    NSLayoutAttributeBottom,    对象下边距

    NSLayoutAttributeLeading, 对象左边距

    NSLayoutAttributeTrailing, 对象右边距 这里特别将强调一下trailing和right代表的意思是一样的 但是实际应用的时候 不能混用 一定要统一

    NSLayoutAttributeWidth, 对象的宽

    NSLayoutAttributeHeight, 对象的高

    NSLayoutAttributeCenterX, 对象中心点的x

    NSLayoutAttributeCenterY,   对象中心的点y

    NSLayoutAttributeLastBaseline,文本下划线

    NSLayoutAttributeBaseline NS_SWIFT_UNAVAILABLE("Use 'lastBaseline' instead") = NSLayoutAttributeLastBaseline,文本下划线

    NSLayoutAttributeFirstBaseline NS_ENUM_AVAILABLE_IOS(8_0),文本上划线

    对于下面的属性在上面的基础上加了边缘,也就是说上下左右距离实际边缘有8磅的距离。

    NSLayoutAttributeLeftMargin NS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeRightMargin NS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeTopMargin NS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeBottomMargin NS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeLeadingMargin NS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeTrailingMargin NS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeCenterXWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeCenterYWithinMargins NS_ENUM_AVAILABLE_IOS(8_0),

    NSLayoutAttributeNotAnAttribute = 0 没有属性


    好了说了那么多废话,终于要应用了,这样吧随便举个例子:在屏幕中央并列横排放三个label,所有的宽与所有的高都相同,居左8,居右8,间距10。

    下面是实现代码:

    UILabel *label1 = [[UILabel alloc] init];

    UILabel *label2 = [[UILabel alloc] init];

    UILabel *label3 = [[UILabel alloc] init];

    [self.view addSubview:label1];

    [self.view addSubview:label2];

    [self.view addSubview:label3];

    label1.translatesAutoresizingMaskIntoConstraints = NO;

    label2.translatesAutoresizingMaskIntoConstraints = NO;

    label3.translatesAutoresizingMaskIntoConstraints = NO;

    label1.backgroundColor = [UIColor greenColor];

    label2.backgroundColor = [UIColor yellowColor];

    label3.backgroundColor = [UIColor redColor];

    label1.text = @"第一个";

    label2.text = @"第二个";

    label3.text = @"第三个";

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:label1 attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];//label1 高=宽

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label1 attribute:NSLayoutAttributeLeftMargin relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeftMargin multiplier:1 constant:0]];// label1 距左8

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label1 attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1 constant:0]];//label1 距上150

    

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:label2 attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];//label2 高=宽

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label2 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:label1 attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];//label2 宽=label1 的宽

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label2 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:label1 attribute:NSLayoutAttributeTop multiplier:1 constant:0]];//label2 上 = label1上边距

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label2 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:label1 attribute:NSLayoutAttributeTrailing multiplier:1 constant:10]];//label2 左距laebl1右边10

    

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label3 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:label3 attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];//label3 高=宽

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label3 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:label1 attribute:NSLayoutAttributeWidth multiplier:1 constant:0]];//label3 宽=label1的宽

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label3 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:label1 attribute:NSLayoutAttributeTop multiplier:1 constant:0]];//label3 上 = label1上边距

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label3 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:label2 attribute:NSLayoutAttributeTrailing multiplier:1 constant:10]];////label3 左距laebl2右边10

    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:label3 attribute:NSLayoutAttributeTrailingMargin relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailingMargin multiplier:1 constant:0]];//label3 距右边8


         下面是效果图:


  其实呢这个方法虽然好用,但是代码量略多,显得臃肿,下期给大家介绍一个第三方工具Masonry的使用。




iOS自动布局代码实现NSLayout 1.首先需要设置view的属性:translatesAutoresizingMaskIntoConstraints设置为NO。因为会和Autoresizing冲突,不设置会报错。 2.约束为NSLayoutConstraint类,创建一个约束对象,通过[constraintWithItem:7个参数]这个方法初始化约束对象。 3.初始化中7个参数说明: 3-1:constraintWithItem:当前设置约束的对象自己。 3-2:attribute:当前设置约束... 阅读详情

相关推荐

车联网ECU、TSP与TBOX通信流程

对比维度ECU(电子控制单元)BMS(电池管理系统)应用场景传统燃油车 + 新能源汽车仅限新能源汽车核心作用控制整车各系统运行管理动力电池安全与寿命车联网角色数据采集与传输的核心载体提供电池相关的核心数据。

tecoes的博客 1233

IOS 添加自动布局约束NSLayoutConstraint

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

路人甲 882

A-Level CS 计算机科学 考试知识点——考试介绍&第1章

前言:该系列文章是本人在给学生辅导本课程的过程中结合英文教材所总结的,该系列共分为30章,主要介绍了考试的核心知识点。若需要A-Level CS 计算机科学考试辅导也可以私信我。 目录 关于A-Level CS 计算机科学考试 考试简介 考试内容 考试时间 评分情况 第1章 信息表示 本章学习内容 1.01 数字系统 1.02 数字的内部编码 整数编码(Coding for integers) 二进制编码的十进制(Binary coded decimal——BCD) 1.03.

王俊的博客 6458

NSLayoutConstraint代码篇

AutoLayout的三种设置方式之——NSLayoutConstraint代码篇 AutoLayout是从IOS 6开始苹果引入来取代autoresizing的新的布局技术,该技术有三种设置方式,等下我来为大家一一叙述一下。 在说三种设置方式前,我们先简单的说一下autolayout能够设置哪些行为。 1.视图的大小(即视图的绝对大小)。 2.视图的位置(视图相对于父

snow_moon_night的专栏 649

Flowable多数据源实战:从配置到指定Schema的避坑指南

本文详细介绍了Flowable多数据源配置的实战指南,包括基础配置、数据源Bean创建、指定Schema的进阶配置以及常见问题排查。通过具体代码示例和避坑建议,帮助开发者高效实现多租户系统中的工作流引擎集成,特别强调了schema设置和事务管理的注意事项。

weixin_30824599的博客 486

实现两端固支梁热力耦合的有限元分析

在本文中,我们将使用Matlab实现两端固支梁的热力耦合有限元分析。我们假设梁的材料是均匀的,梁的截面积为1,长度为1,两端固支,且受到均匀分布的热荷载。接下来,我们需要组装全局刚度矩阵和热传导矩阵。对于两个相邻的元素,它们共享一个节点,因此需要使用连接矩阵将局部矩阵组装成全局矩阵。接下来,我们需要定义元素刚度矩阵和热传导矩阵。然后,我们需要定义载荷向量。然后,我们需要定义边界条件。在这个例子中,梁的两端固支,因此我们需要将两端节点的位移限制为0。最后,我们需要解决线性方程组,得到节点的位移和温度分布。

CyberSparkZ的博客 396

NSLayoutConstraint约束

相信ios8出来之后,不少的ios程序员为了屏幕的适配而烦恼。相信不少的人都知道有AutoLayout 这么个玩意可以做屏幕适配,事实上,AutoLayout不仅仅只是一个为了多屏幕适配的工具, 它真正的意义所在是给了程序员一种全新的布局思想。    本文主要依据真实项目实例从三个方向全方位讲解AutoLayout的使用大全。    一。AutoLayout布局原理和语法    二。约束

shuju345的博客 716

工业级向量检索核心技术:IVF-PQ原理与全流程解析

在大规模高维向量检索场景中,IVF-PQ(倒排索引结合乘积量化)凭借“粗筛范围+细算距离”的双重加速策略,成为核心解决方案。本文将深入解析其原理与全流程,助力开发者掌握这一关键技术。

进一步有进一步的欢喜~ 1197

AutoLayout - NSLayoutConstraint

什么是NSLayoutConstraint NSLayoutConstraint由名字翻译过去就是布局约束。该类是表示用户界面上两个对象之间的布局关系,并且该关系必须满足基本约束的布局系统。 解析约束(Constraint) 布局视图(view)定义为一系列的线性方程,每一个约束(constraint)都代表一个方程,格式如下: 上面的约束表示RedView的左边距(left或

欢迎来到Jack的程序世界 597

推荐系统[九]项目技术细节讲解z2:搜索Query理解[Term Weight、Query 改写、同义词扩写]和语义召回技术

在 pooling 方法上,我们也尝试过不同层 pooling,或者在多层上增加 attention 聚合 BERT 多层结果,效果和最后一层所有 token 做 average pooling 相当。损失函数方面,在排序阶段使用pairwise比 pointwise 损失函数的效果会更好。

丨汀、的博客 1109

点云处理实操(四) -PCL中的点云三角化

点云处理实操(四) -PCL中的点云三角化

cangqiongxiaoye的博客 939

Swift代码动态创建NSLayoutConstraint布局

示例效果 说明 黄色lable为子view(childView),红色view为父view(parentView)。两者关系为: childView在距离父控件的左右各20边距处, 高度25。chidView在parentView中垂直方向上居中显示。注:约束的设置,控件内部约束由自己添加,比如宽高,如果是与其他的 控件约束那么有父控件添加NSLayoutConstraint参数说明:/**

heyufeiGuide 2054

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

告别AutoLayout痛点:NSLayoutConstraint优雅布局指南

你是否还在为AutoLayout的警告抓狂?是否曾因约束冲突导致界面错乱?本文将带你掌握NSLayoutConstraint(自动布局约束)的核心用法,用符合[官方风格指南](https://link.gitcode.com/i/e5a983546834a03ab5811146101c44bd)的方式构建灵活可靠的iOS界面。读完你将学会:3种创建约束的方法对比、约束冲突调试技巧、以及如何用代码实...

gitblog_00530的博客 849

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

通过代码 新增 和 修改 NSLayoutConstraint

通过代码 新增 和 修改 NSLayoutConstraint

qq_43668118的博客 374

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

NSLayoutConstraint

AutoLayout的三种设置方式之——NSLayoutConstraint代码篇,nslayoutconstraint AutoLayout是从IOS 6开始苹果引入来取代autoresizing的新的布局技术,该技术有三种设置方式,等下我来为大家一一叙述一下。 在说三种设置方式前,我们先简单的说一下autolayout能够设置哪些行为。 1.视图的大小(即视图的绝对大小)

kaplaSxiao的博客 509

通过代码 新增 和 “修改”NSLayoutConstraint

今天因为要弄一个瀑布流里面item的动态加载, 所以考虑把里面的一些空间的高度设成0从而实现“隐藏”效果 网上对这类修改是 关联 NSLayoutConstraint 然后设置对应的constant 下面是网上的答案,但是注意了,要用父类layoutIfNeeded()来重新布局 0down vote If you are adding co

飞翔的熊blabla 6346

使用JGLayoutDotSyntax的常见坑与最佳实践清单:约束冲突、优先级与动态约束避坑指南

JGLayoutDotSyntax 是一个面向 iOS 的 Auto Layout 辅助库:用一行点语法(dot syntax)替代 NSLayoutConstraint 的冗长写法,原生支持 constant、multiplier 与优先级,还提供基于 KVO 的动态约束和字体尺寸联动。本文按**约束冲突、优先级、动态约束**三大主题,整理新手最容易踩的 9 个坑,并给出速查清单。 ## 点语

gitblog_00895的博客 178

iphone4、4S程序如何兼容Iphone5 (关键字 NSLayout、NSLayoutConstraint)

1.首先记录如何让程序兼容iphone5,很容易搜索到,此处简单记录。 1.xib中use Autolayout勾上 2.xib中Size--Retina 4 Full Screen 3.代码中判断是否是iphone5:---- #define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)]

kareily的专栏 1603

IOS 自动布局-UIStackPanel和UIGridPanel(二)

zhan.renren.com/705071191?gid=3602888498058355897 zhan.renren.com/705071191?gid=3602888498058355848 zhan.renren.com/705071191?gid=3602888498058355840 zhan.renren.com/705071191?gid=36028884980583558

86

关于iOS代码实现自动布局

这里做一个通过代码实现自动布局的Demo,通过IB来做的就不讲了,网上相关的资料很多,这里给出一个写的不错的链接,有兴趣的同学自己看吧. iOS7自动布局教程(一) iOS7自动布局教程(二)    --英文 要谈自动布局,那基本的视图是第一步,做了一个这样的ViewController // // NESMainViewController.m // AutoLayout

pxd123147的专栏 920

九宫格布局

#import "SFViewController.h" @interface SFViewController () @property (nonatomic,strong)NSArray * array; @end @implementation SFViewController -(NSArray *)array {     if(_array ==n

sun520000fei的专栏 352

iOS自动布局学习笔记

IOS自动布局学习笔记

qq_45283740的博客 394

UI自动布局

当内部或外部条件发生变化的时候,自动重新计算视图的位置和大小。对于ios应用,可以触发自动布局的变化可以分为外部变化和内部变化: 自动布局需要通过约束constraint来实现。下面,先说明使用约束来实现自动布局的原理,后说明如何创建、修改、查看约束。

ohyeahhhh的博客 3640
上一篇: 分组的自定义流布局方法二
下一篇: Masonry使用心得
wjymark
博客等级 码龄10年 7粉丝 13原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值