在iOS中,自动调整大小,AutoLayout和约束之间有什么区别? [英] In iOS, what's the difference between autoresizing, AutoLayout, and constraints?

查看:139
本文介绍了在iOS中,自动调整大小,AutoLayout和约束之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读Apple文档时,我发现了对自动调整,AutoLayout和约束的引用。在代码中使用所有这些之间有什么区别?在iOS 9中以编程方式应用这些技术的正确方法是什么?

In reading through the Apple documentation, I find references to autoresizing, AutoLayout, and constraints. What's the difference between using all of these in code? What is the correct way to apply these techniques programmatically in iOS 9?

推荐答案

这里只有两件事:


  • 自动调整

  • AutoLayout

Autoresizing 基本上是Apple为了使开发人员能够构建动态布局而引入的旧方式的统称。这里要解决的第一个用例是屏幕旋转。由于当屏幕旋转(或以其他方式调整大小)时,屏幕中的子视图很可能在新尺寸的超视图中保持不正确的框架(位置和大小)。为了解决这个问题,Apple引入了一系列可枚举的属性(称为 Autoresizing Masks ),告诉superview以特定方式处理子视图。其中包括:

Autoresizing is basically a collective term for the old way Apple introduced in order to enable developers to build dynamic layouts. The number one usecase to address here was screen rotation. Since when a screen would be rotated (or otherwise resized), the subviews in the screen would most likely hold an incorrect frame (position and size) in the newly sized superview. To address this, Apple introduced a series of enumerable properties (called Autoresizing Masks), that tell the superview to treat a subview in a particular way. Among these are:


  • 灵活宽度/高度,这会导致子视图扩展到可用的最大宽度/高度

  • Flexible Width/Height, which causes a subview to expand to the fullest width/height available

灵活的前导/尾随/上/下空间,允许特定边缘变化,等等。

Flexible Leading/Trailing/Top/Bottom space, which allows a specific edge to be variable, and so forth.

视图可以包含这些枚举属性的任意组合。

A view could contain any combination of these enum properties.

这是不充分的,因为除了其他缺点之外,它没有规定关于如何在其他兄弟观点中布置视图(如果这是动词)的规则。它还需要大量额外的编码来手动调整方向更改的视图。

This was inadequate because, among other shortcomings, it lays down no rules regarding how a view should be layouted (if that's a verb) with respect to its other sibling views. It also required a lot of extra coding to manually resize views on orientation changes.

这是AutoLayout进入图片的位置。 Apple构建了一个基于约束的框架 - 可以应用于视图和视图之间的规则,这将决定视图在可变屏幕大小中的大小。这些约束在一个名为 NSLayoutConstraint 的类中构造,并且此类的每个实例(约束)都具有以下重要属性:

Here's where AutoLayout entered the picture. Apple built a framework which worked on the basis of constraints - rules that could be applied on views and between views, that would determine how a view would be sized in variable screen sizes. These constraints are structured in a class called NSLayoutConstraint, and each instance (constraint) of this class has the following important properties:


  • 应用约束的项目(视图)

  • 视图的属性(高度,宽度,前缘,后缘等等) on)约束适用于

  • 与约束相关的第二项(兄弟或子视图或父视图)

  • 第二项的属性

  • 约束的乘数:用于指定基于比率的约束

  • 一个值(或约束的常量):有趣的是,实例化后可以更改约束的唯一属性。

  • The item (view) on which the constraint is applied
  • The property of the view (height, width, leading edge, trailing edge, and so on) that the constraint is applicable to
  • The second item (a sibling or a child or a parent view) to which the constraint is related
  • The second item's attribute
  • The multiplier on the constraint: useful in order to specify ratio based constraints
  • A value (or constant) of the constraint: interestingly, the only property of a constraint that can be changed after instantiation.

一个简单的 NSLayoutConstraint 示例,具体说明:视图的宽度将是其超级视图宽度的一半乘以60%。

A simple example of an NSLayoutConstraint, stated prosaically is: a view's width will be half the the width of its superview multiplied by 60%.

您的基于AutoLayout的UI将包含许多此类约束,这些约束将共同表达一个明确的和非冲突的UI布局。

Your AutoLayout based UI would consist of many such constraints, which will all work together to express an unambiguous and non-conflicting UI Layout.

现在 AutoLayout引擎,它使一切正常,与屏幕上的视图交互,调用AutoLayout消息,如as layoutSubviews 每当需要时,屏幕上发生变化时自动调整大小(布局)视图,例如方向更改,调整大小的超级视图等。

Now the AutoLayout engine, which makes it all work, interacts with views on the screen, calling AutoLayout messages such as layoutSubviews whenever needed in order automatically resize (layout) views whenever changes occur on screen, such as orientation change, a superview getting resized etc.

约束最常由InterfaceBuilder(.xib和.storyboard文件)添加,但是按代码添加它们需要相同的原则:创建 NSLayoutConstraint 并将其添加到适用的最高视图(例如,如果孩子之间存在约束)在父视图中,应将约束添加到父视图中。如果两个子视图之间存在约束,请再次将其添加到父级。)

Constraints are most commonly added by InterfaceBuilder (.xib and .storyboard files), but adding them by code entails the same principle: create an instance of NSLayoutConstraint and add it to the highest view applicable (for eg., if there's a constraint between a child and a parent view, the constraint should be added to the parent view. If there's a constraint between two subviews, again, add it to the parent.)

Apple的 AutoLayout指南 API文档和关于AutoLayout的介绍性 WWDC视频非常好,这些将是了解更多信息的最佳场所。

Apple's AutoLayout guides, API documentation and introductory WWDC videos on AutoLayout are excellent, and those would be the best places to learn more.

这篇关于在iOS中,自动调整大小,AutoLayout和约束之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆