iOS:Autolayout自动布局实例






















#import "ViewController.h" @interface ViewController ()
@property (strong,nonatomic)UIView *view1;
@property (strong,nonatomic)UIView *view2;
@end
//创建view1
self.view1 = [[UIView alloc]init];
self.view1.backgroundColor = [UIColor redColor];
self.view1.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.view1]; //创建view2
self.view2 = [[UIView alloc]init];
self.view2.backgroundColor = [UIColor yellowColor];
self.view2.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.view2];
//view1的x坐标和父视图的x坐标的关系
NSLayoutConstraint *LCLfet = [NSLayoutConstraint constraintWithItem:self.view1 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:];
4.创建view1和父视图的y坐标之间的约束
//view1的y坐标和父视图的y坐标的关系
NSLayoutConstraint *LCBottom = [NSLayoutConstraint constraintWithItem:self.view1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-];
//view1和view2等宽
NSLayoutConstraint *lcEqualWitdth = [NSLayoutConstraint constraintWithItem:self.view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view2 attribute:NSLayoutAttributeWidth multiplier:1.0 constant:];
//view1和view2等高
NSLayoutConstraint *lcEqualHeight = [NSLayoutConstraint constraintWithItem:self.view2 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view1 attribute:NSLayoutAttributeHeight multiplier:1.0 constant:];
//view2的x坐标和父视图的x坐标的关系
NSLayoutConstraint *LCRight = [NSLayoutConstraint constraintWithItem:self.view2 attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:-];
8.创建view2和父视图的y坐标之间的约束
//view2的y坐标和父视图的y坐标的关系
NSLayoutConstraint *LCBottom2 = [NSLayoutConstraint constraintWithItem:self.view2 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-];
//view1和view2的水平间距
NSLayoutConstraint *lcGap = [NSLayoutConstraint constraintWithItem:self.view2 attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view1 attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:];
//在父视图中添加约束
[self.view addConstraints:@[LCLfet,LCBottom,LCRight,LCBottom2,lcEqualHeight,lcEqualWitdth,lcGap]];
//view1固定的高度
NSLayoutConstraint *lcFixedHeight = [NSLayoutConstraint constraintWithItem:self.view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:];
//在view1上添加约束
[self.view1 addConstraint:lcFixedHeight];
需求:view1使得它与上方的距离20,左方、右方的距离各为20,高50,view2在view1下方距离20,右边距离父视图20(与view1右对齐),view2的宽度是view1的一半。
实现:
(1)先固定view1,设定左右上的约束并设定高为50.

(2)这个需求的重点是设置view2,首先与view1上方距离20,右边与view1对齐,那么宽度的设置体现了Autolayout的精华所在。
方案一:先设置view2与view1等宽,之后双击view2的等宽的线,将multiplier的值设为0.5。

1)设置view2的居中对齐


AutoLayout核心公式
第一个Item的属性 =(<=/>=)第二个Item的属性*Multiplier+Constant
这就是Autolayout的精华所在.
属性说明:
- Leading Edges:左对齐
- Trailing Edges:右对齐
- Top Edges:上对齐
- Bottom Edges:下对齐
- Horizontal Centers:水平中心对齐
- Vertical Centers:竖向中心对齐
- Baselines:基线对齐
- Horizontal Center in Container:对齐容器中的水平中心
- Vertical Center in Container:对齐容器中的竖向中心
警告与报错
警告:一般是黄色的,一般是由于我们当前所设的约束与当前视图的位置不符。报错:红色的警告一般是由于约束错误造成的,这种警告工程中一定要消除。
总结
- Autoresizing:
已经是比较过时的设置适配方法了,而且有很大的缺陷,只能设置父子控件之间的约束,不能设置兄弟控件之间的约束。所以在这里我们最好不要再开发中应用。 - AutoLayout:
最流行的适配方式,苹果积极推荐,非常强大好用的适配方法。对提升开发中的效率有很大的帮助。
iOS:Autolayout自动布局实例的更多相关文章
- iOS AutoLayout自动布局&Masonry介绍与使用实践
Masonry介绍与使用实践:快速上手Autolayout http://www.cnblogs.com/xiaofeixiang/p/5127825.html http://www.cocoachi ...
- AutoLayout(自动布局)
1. iOS两种自适应布局方式:(修正说明:) -AutoLayout(自动布局) + SizeClasses(尺寸类别) -Autoresizing (自动调整尺寸/弹簧式调整尺寸) 前者 Auto ...
- 从此爱上iOS Autolayout
转:从此爱上iOS Autolayout 这篇不是autolayout教程,只是autolayout动员文章和经验之谈,在本文第五节友情链接和推荐中,我将附上足够大家熟练使用autolayout的教程 ...
- iOS开发-自动布局篇:史上最牛的自动布局教学!
转载自:http://www.jianshu.com/p/f6cf9ef451d9 本文我们将提到: aotulayout(手码) VFL aotulayout(Xib) Masonry(第三方框架) ...
- IOS之UI--小实例项目--综合使用
前言: 本博文是基于前一个小实例项目:IOS之UI--小实例项目--添加商品和商品名 进行继续综合学习积累的. 内容大纲 01-综合使用01-plist的使用 02-综合使用02-模型取代字典的好处分 ...
- 【转】IOS AutoLayout详解(三)用代码实现(附Demo下载)
转载自:blog.csdn.net/hello_hwc IOS SDK详解 前言: 在开发的过程中,有时候创建View没办法通过Storyboard来进行,又需要AutoLayout,这时候用代码创建 ...
- iOS autoLayout总结
本文转自 http://ruikq.github.io/ios/autolayout/uiscrollview/2015/01/27/iOS-autolayout%E6%80%BB%E7%BB%93. ...
- iOS:自动布局Autolayout
自动布局:Autolayout 简介: 在以前的iOS程序中,是如何设置布局UI界面的? 经常编写大量的坐标计算代码 为了保证在3.5 inch和4.0 inch屏幕上都能有完美的UI界面效果,有时还 ...
- iOS: 学习笔记, 用代码驱动自动布局实例(swift)
iOS自动布局是设置iOS界面的利器.本实例展示了如何使用自动布局语言设置水平布局, 垂直布局1. 创建空白iOS项目(swift)2. 添加一个控制器类, 修改YYAppDelegate.swift ...
随机推荐
- 二叉查找树(BST)
二叉查找树(BST):使用中序遍历可以得到一个有序的序列
- C# 把引用的dll嵌入到exe文件中
当发布的程序有引用其它dll, 又只想发布一个exe时就需要把dll打包到exe 当然有多种方法可以打包, 比如微软的ILMerge,混淆器附带的打包... 用代码打包的实现方式也有很好,本文只是其中 ...
- zabbix语言设置
1.怎样支持中文: https://www.zabbix.org/wiki/How_to/install_locale 官方解决方法 实际操作中,进入/var/lib/locales/supporte ...
- ReactJS入门
React介绍 React是facebook开发基于组件驱动开发(CDD ) 的UI类库,相对于双向绑定的类库,如AngularJS,它采用单向数据流绑定.通过采用虚拟DOM的概念,是的他在性能和处理 ...
- cygwin and its host machine
Senario 本来我是想要修改下 machine name 在Ubuntu中的步骤是这样的 1 sudo hostname newMechineName 2 sudo vi /etc/hostnam ...
- 【BZOJ】【2480】【SPOJ 3105】Mod
扩展BSGS Orz zyf……然而他的题解对AC大神的题解作了引用……而坑爹的百度云……呵呵了... 扩展BSGS模板题 /************************************* ...
- 剑指offer--21题
#include "stdafx.h" #include<iostream>using namespace std; void LeftRotateString(cha ...
- windows下安装php5.2.*,php5.3.*,php5.4.*版本的memcache扩展(转)
拓展安装调试方法: 编写调试php文件 <?php memcache(); 通过命令行执行测试,因为php拓展安装成功与否与apache无关,所以没必要不断重启apache去看phpinfo ...
- HDOJ 2079 选课时间(母函数)
选课时间(题目已修改,注意读题) Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- MYSQL注入天书之服务器(两层)架构
Background-6 服务器(两层)架构 首先介绍一下29,30,31这三关的基本情况: 服务器端有两个部分:第一部分为tomcat为引擎的jsp型服务器,第二部分为apache为引擎的php服务 ...