最近做的项目用到了Auto Layout,于是经过了一番大量的google,这是我看到的讲用代码创建约束最清晰的一篇教程,于是想跟更多的人分享一下。原文也比较简单,可以直接过去看,如果我翻译的那块需要校对的,也请多多指教。

原文:http://www.ioscreator.com/tutorials/auto-layout-in-ios-6-adding-constraints-through-code

iOS6提供了一种设计用户界面的新方法:Auto Layout。使用Auto-Layout很容为多种屏幕大小和多种语言设计UI。你可以在IB中使用Auto Layout,那么你一定要小心,否则不经意地移动了界面上的一个UI组件就会弄乱这些约束。因此对于这篇教程,我们用代码定义约束。


开XCode并创建一个Single View
Application。工程名叫作ConstraintsCodeDemo,然后填上组织名,公司标识,类前缀。确定在Devices中只选择了
iPhone,Use Storyboards没有选,并且Use Automatic Reference Counting选上了。

我们将创建一个简单的按钮,并想把它放在界面的左上角。在viewController.m中创建如下实例变量

ViewController

{

UIButton *firstButton;

在viewDidLoad中创建这个按钮,并把它添加到view中

- (void)viewDidLoad

{

[superviewDidLoad];

firstButton = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];

[firstButton setTitle:@"First"forState:UIControlStateNormal];

[firstButton sizeToFit];

firstButton.translatesAutoresizingMaskIntoConstraints = NO;

[self.view addSubview:firstButton];

  这是由于苹果在iOS 6当中引入了自动布局的新概念,但在那时仍然有很多旧的代码使用autoresizingMask与setFrame:的方式构建界面。试想,如果将一个 已经设置好frame并使用autoresizingMask的视图添加到一个使用自动布局的视图中时,运行时需要隐式地将前者的frame和 autoresizingMask转化为自动布局约束(这些隐式转换的约束的类型为 NSAutoresizingMaskLayoutConstraint),这样才能明确其位置与尺寸而不会导致约束的缺失。这个隐式转换的过程,是由 UIView的translatesAutoresizingMaskIntoConstraints属性的值决定的。默认情况下,该值为YES,表示需 要运行时自动进行隐式转换。这对于兼容旧的代码当然是好的,然而当我们明确为视图添加了约束后,我们就不希望再进行autoresizingMask的隐 式转换了,否则就会引起约束的冲突。因此,需要特别注意的是,当我们使用代码创建视图时,需要将translatesAutoresizingMaskIntoConstraints属性的值设置为NO

设置translatesAutoresizingMaskIntoConstraints属性为NO,来禁用“古老的”springs and struts方法。这个属性是为了向后兼容。我们将添加一个左边约束,于是这个按钮会在view中移动20个点。把下面的代码添加到viewDidLoad下面。

*constraint = [NSLayoutConstraint constraintWithItem:firstButton attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqualtoItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:20.f];

[self.view addConstraint:constraint];

Auto Layout用一个公式来计算界面之间的关系:

A = B * m + c

或者

viewA.attribute = viewB.attributs*multiplier + constant

在这个例子中公式如下:

firstButton.NSLayoutAttributeLeading = self.view.NSLayoutAtrributeLeading * 1.0f + 20f

按钮左边的位置等于父视图左边距减掉20.

把下面的约束添加到viewDidLoad里

= [NSLayoutConstraint constraintWithItem:firstButton attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqualtoItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:30.f];

[self.view addConstraint:constraint];

该约束将会使按钮距离父视图上面30个点。Build and Run。这个按钮距离左边20个点,上面30个点。

按 cmd+<- 使界面横屏。

在interface中创建一个新的实例变量

*secondButton;

在viewDidLoad中添加下面的代码,在view中添加第二个按钮。

= [UIButton buttonWithType:UIButtonTypeRoundedRect];

[secondButton setTitle:@"Second"forState:UIControlStateNormal];

[secondButton sizeToFit];

secondButton.translatesAutoresizingMaskIntoConstraints =NO;

[self.view addSubview:secondButton];

这个按钮将在父视图的X轴方向居中,距离底部40个点。在viewDidLoad中添加下面的约束。

= [NSLayoutConstraint constraintWithItem:secondButtonattribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqualtoItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0fconstant:-40.f];

[self.view addConstraint:constraint];

= [NSLayoutConstraint constraintWithItem:secondButton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0fconstant:0.0f];

[self.view addConstraint:constraint];

第二个按钮宽度固定为200个点。添加下面约束。

= [NSLayoutConstraint constraintWithItem:secondButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqualtoItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0fconstant:200.0f];

[self.view addConstraint:constraint];


注意这个约束只与按钮有关,与父视图无关,因为这是一个固定大小。因此toItem设置为nil,对应的属性参数设置为
NSLayoutAttributeNotAnAttribute。Build and
Run。这个固定的按钮放在X轴的中心,并且距离父视图底部40个点。

按cmd+<- 使界面横屏。

以上是在代码中使用约束的一个简介,还有更多的内容需要理解,但是这篇教程的目的是使事情能够简单,期待着在将来的教程中有更复杂的例子。

你可以在github上下载到ConstraintsCodeDemo的源代码。

【转】iOS6中的Auto Layout:通过代码添加约束的更多相关文章

  1. iOS6 自动布局 入门–Auto Layout(转)

    iOS6 自动布局 入门–Auto Layout(转) 标签: 杂谈   目前为止,即使你的界面设计是在合理的复杂度内,你也必须要为之写许多代码来适应变化的布局.现在我相信你会很高兴听到这种情况将不会 ...

  2. iOS6 自动布局 入门–Auto Layout

    目前为止,即使你的界面设计是在合理的复杂度内,你也必须要为之写许多代码来适应变化的布局.现在我相信你会很高兴听到这种情况将不会发生了-对于iPhone与iPad IOS6 带来了一个非常了不起的特征: ...

  3. Auto Layout之创建布局约束

    上篇文章给大家介绍了AutoLayout 的由来及OC中关于AutoLayout 的类.这篇文章将向大家介绍,在iOS 编程中怎样使用Auto Layout 构建布局约束. 创建布局约束 创建布局约束 ...

  4. Visual Studio中使用Macros插件给代码添加注释、时间和以及自动脚本

    title: Visual Studio中使用Macros插件给代码添加注释.时间和以及自动脚本 date: 2020-09-11 sidebarDepth: 2 tags: 代码 Visual st ...

  5. 【转】iOS学习之Autolayout(代码添加约束) -- 不错不错

    原文网址:http://www.cnblogs.com/HypeCheng/articles/4192154.html DECEMBER 07, 2013 学习资料 文章 Beginning Auto ...

  6. Sql语句在SqlServer中创建数据库、表格并添加约束

    通过Sql语句来创建数据库与架构 创建数据库 数据库的创建首先是要引用主数据库的,需要在master数据库的环境下进行创建.大致的语法如下: -- 使用master数据库 use master -- ...

  7. IOS NSLayoutConstraint 页面布局(通过代码添加约束)

    #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) UIVi ...

  8. 【转】Auto Layout 进阶

    原文:http://blog.csdn.net/ysy441088327/article/details/12558097   引言: Auto Layout是iOS6发布后引入的一个全新的布局特性, ...

  9. iOS开发之Auto Layout入门(转)

    随着iPhone6与iOS8的临近,适配的问题讲更加复杂,最近学习了一下Auto Layout的使用,与大家分享. 什么是Auto Layout? Auto Layout是iOS6发布后引入的一个全新 ...

随机推荐

  1. cf D. On Sum of Fractions

    http://codeforces.com/problemset/problem/397/D 题意:v(n) 表示小于等于n的最大素数,u(n)表示比n的大的第一个素数,然后求出: 思路:把分数拆分成 ...

  2. 工作那些事(二)应聘时填写个人信息ABCD

    先看看都有那些: 公司A: 填写来访人员登记表(在前台的那种),内容包括: 姓名.时间.电话.职位. 公司B: 填写来访人员登记表(在前台的那种),内容包括: 姓名.时间.电话.身份证号码().事由( ...

  3. Buffer Sort

    BUFFER (SORT) Description Performs a memory sort on a row source CREATE TABLE t1 (c01 NUMBER); CREAT ...

  4. COJ 2135 Day10-例1

    Day10-例1 难度级别:B: 运行时间限制:1000ms: 运行空间限制:256000KB: 代码长度限制:2000000B 试题描述 给定N个物品,价格分别为A1, A2…AN.设计一套面值互不 ...

  5. Ganglia监控搭建

    一.Ganglia介绍: Ganglia是一个监控服务器.集群的开源软件,能够用曲线图表现最近一个小时,最近一天,最近一周,最近一月,最近一年的服务器或者集群的cpu负载,内存,网络,硬盘等指标.Ga ...

  6. CTE Recursion Performance

    CTE全名是Common Table Expression,语法基础请参考MSDN文档:https://msdn.microsoft.com/zh-cn/library/ms175972.aspx. ...

  7. 差别不在英语水平,而在汉语水平If you do not leave me, we will die together.

    为什么高考语文要提高到180分,英语降到100,差别不在英语水平,而在汉语水平.看下面例句的译法: If you do not leave me, we will die together. 你如果不 ...

  8. Mac下Intellij IDea发布Java Web项目详解四 为所有Module配置Tomcat Deployment

    准备工作1:新建第一个JavaWeb项目 准备工作2:新建Module step5 为所有项目配置Deployment 5.1 如图 5.2 [+][Artifact] 5.3 将这里列出的所有内容选 ...

  9. zoj 2706 线段树

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1706 trick:关于正数和负数的整除问题,正数整除是自动向下取整的 ...

  10. flumeng-kafka-plugin

    github 参考地址:https://github.com/beyondj2ee/flumeng-kafka-plugin/tree/master/flumeng-kafka-plugin /* * ...