Auto Layout Concepts

auto layout的基本概念是constraint(约束)。表示了你interface中的layout元素。例如,你可以创建一个约束来指定元素的宽度或者距离另一个元素的水平距离。你可以添加或者移除约束或者改变约束的属性来作用你的interface的布局。

当计算user interface运行时候的位置时,auto layout系统会在同一时间考虑所有的约束并且设置能最好满足约束的位置。

Constraint Basics

属性囊括left, right, top, bottom, leading, trailing, width, height, centerX, centerY, baseline. constant value: 以points为单位的物理大小或者offset. Relation: Auto layout支持更多。你可以使用关系和不等式比如greater-than-or-equal来说明。例如,一个view的宽度>=20。priority level。约束有优先级。默认的优先级是必须的。(NSLayoutPriorityRequired),意思是约束必须完全满足。layout系统会尽可能的满足一个可选的约束,即使不能完全实现这个约束。

优先级允许你表达有用的条件选择。例如,他们被用来表示一些控件应当总是重置大小来填充他们的contents。除非一些更加重要的出现。

约束是积累的,并且不会彼此覆盖。如果你有一个现存的约束,设置同样类型的约束不会覆盖之前的。例如,设置一个view的第二个宽度约束不会移除或者改变第一个约束宽度。你需要手动移除第一个约束。

约束可以和一些限制条件贯穿存在于view层中。在os x的mail app中,例如,默认的删除按钮在toolbar里与信息一起。

你不能设置贯穿view层的约束如果层中包含一个在layoutSubviews方法中自定义的手动设置的subviews。也不可能贯穿有bounds改变的人和views,例如scroll view。你可以认为这种情况下,有一个里面和外面的世界,但是里面的世界无法通过约束来连接到外面的世界。

Intrinsic Content Size

leaf-level views例如buttons比代码更清楚应当在什么位置。这通过intrinsic content size来告知layout系统一个view包含一些content并不是由来就被理解的并且指示这个content多大。

再比如text labels等,你应当设置元素的intrinsic size通过Editor->Size To Fit Content.这个意味着元素将grow并且shrink根据不同语言的content.

Working with Constraints in Interface Builder

添加,编辑或者移除约束的最简单方法是使用interface builder中的可视化layout tools。你可以简单得使用不同的pop-up窗口。

Adding Constraints

当添加好一个控件,但是并没有添加约束,你会发现运行时候不论device的方向时怎样的,这个控件的宽度和高度总是一样的。而且总是记住了左上角的相对位置。即便重置window的大小也无法移除或者重置元素的大小。为了让你的interface能正确的改变大小或者方向,你就需要开始添加约束了。

下面是几种添加约束的方法。

Adding Constraints with Control-Drag

添加约束最快的方法就是ctrl+drag一个view。

Adding Constraints with Align and Pin Menus

使用auto layout menu功能。

Align : 创建校准约束,例如在容器内居中一个view或者校准两个view的左边缘。
Pin: 创建间隔约束,例如定义一个view的高度或者指定他同其他view的水平间距。
Issues : 解决layout问题通过根据建议添加或者重新设置约束的方法。
Resizing:指出resizing如何作用于约束。
 

如果你只有一个元素被选择,那么约束选项需要多个元素的对应选项就无法选择。

To add a constraint from the Align or Pin menu

1. Select the checkbox next to the appropriate constraint.

To select a “Spacing to nearest neighbor” constraint, select the red constraint corresponding to the appropriate side of the element.

If you need to create a constraint related to another view that is not the nearest neighbor, click the black disclosure triangle in the value text field to display a drop-down menu of other nearby views.

  1. Enter a corresponding constant value.

  2. Press a button to create the constraints.

    The Add Constraints button adds the new constraints to the selected elements.

    The Add and Update Frames button adds the new constraints to the selected elements, and moves the elements in your interface to satisfy every constraint as well as possible.

Autolayout 01的更多相关文章

  1. 在StoryBoard对UICollectionViewCell 进行Autolayout是遇到的Xcode6.01的BUG

    使用Sb对UICollectionViewCell 的内容进行Autolayout约束时候,发现了一个Xcode6.01的BUG,就是你对UICollectionCell约束完了之后,在模拟器上现实的 ...

  2. 16-UIKit(AutoLayout、Animation)

    目录: 一.AutoLayout自动布局 二.动画(Animation) 回到顶部 一.AutoLayout自动布局 1.什么是AutoLayout 从ios6开始引入的新技术,是新版的自动布局技术 ...

  3. 非等高cell实战(01)-- 实现微博页面

    非等高cell实战(01)-- 实现微博页面 学习过UITableView.AutoLayout以及MVC的相关知识,接下来通过一个微博页面实战来整合一下. 首先看一下效果图: 需求分析 此页面为非等 ...

  4. iOS autoLayout总结

    本文转自 http://ruikq.github.io/ios/autolayout/uiscrollview/2015/01/27/iOS-autolayout%E6%80%BB%E7%BB%93. ...

  5. AutoLayout深入浅出五[UITableView动态高度]

    本文转载至 http://grayluo.github.io//WeiFocusIo/autolayout/2015/02/01/autolayout5/ 我们经常会遇到UITableViewCell ...

  6. AutoLayout详解+手把手实战(转载)

    首先说一下这篇博客虽然是标记为原创,但是事实并非本人亲自写出来的,知识点和例子本人花了一天各处查 找和整理最终决定写一个汇总的详解,解去各位朋友到处盲目查找的必要,因为不是转载某一个人的内容,故此不标 ...

  7. AutoLayout具体解释+手把手实战

    首先说一下这篇博客尽管是标记为原创,可是事实并不是本人亲自写出来的.知识点和样例本人花了一天各处查找和整理终于决定写一个汇总的具体解释,解去各位朋友到处盲目查找的必要,由于不是转载某一个人的内容.故此 ...

  8. 01.SQLServer性能优化之----强大的文件组----分盘存储

    汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 文章内容皆自己的理解,如有不足之处欢迎指正~谢谢 前天有学弟问逆天:“逆天,有没有一种方 ...

  9. 用Kotlin创建第一个Android项目(KAD 01)

    原文标题:Create your first Android project using Kotlin (KAD 01) 作者:Antonio Leiva 时间:Nov 21, 2016 原文链接:h ...

随机推荐

  1. 【Jump Game】cpp

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  2. alert(1) to win部分解题

    XSS在线习题分析 (https://alf.nu/alert1) 1. Warmup function escape(s) { return '<script>console.log(& ...

  3. Selenium - WebDriver: Waits

    These days most of the web apps are using AJAX techniques. When a page is loaded to browser, the ele ...

  4. 【转】Unity3D 入门小技巧——鼠标拾取并移动物体

    http://blog.csdn.net/sysujackjiao/article/details/69396274 一.鼠标拾取物体的原理 在Unity3D当中,想要在观察面(Aspect)中拾取物 ...

  5. Unity开发VR——Oculus Rif_将Oculus接入Unity

    该文档基于 Unity2018.3.12f1 1. 搭建简单场景 2. 设置,选择 Edit - Project Setting(若已经勾选,就去掉在勾选一次) 完成该步骤之后,可以带上Oculus头 ...

  6. python基础-集合小结

    Python-基础-集合小结 集合 简介 声明 常用操作 成员关系 新增删除 集合间操作 其他 补充 集合 简介 python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和 ...

  7. 【bzoj3879】SvT 后缀数组+倍增RMQ+单调栈

    题目描述 (我并不想告诉你题目名字是什么鬼) 有一个长度为n的仅包含小写字母的字符串S,下标范围为[1,n]. 现在有若干组询问,对于每一个询问,我们给出若干个后缀(以其在S中出现的起始位置来表示), ...

  8. spring 解决中文乱码问题

    spring 解决中文乱码问题 使用spring的前提下在web.xml中配置 <filter> <filter-name>encodingFilter</filter- ...

  9. ICPC World Finals 2018 Problem H Single Cut of Failure

    题目链接 题解视频 题解文档 解法概要: 问题可以转化为 考虑一个长为 $2n$ 的数组 $A$,$1$ 到 $n$ 这 $n$ 个整数每个恰在 $A$ 中出现 $2$ 次.判断是否存在一个长为 $n ...

  10. [canvas入坑0] Jquery + HTML5 做最简易的画板

    查看效果请到 http://philippica.github.io/  点击paint 嗯,心血来潮想做个东西的一部分 html部分不用多说了,重点就是一个canvas <!DOCTYPE h ...