对iOS 9 新增的控件 UIStackView 官方文档的翻译 第三部分

来源:互联网 发布:淘宝的流量来源 编辑:程序博客网 时间:2024/05/12 15:49

管理 Stack 视图的展现Managing the Stack View’s Appearance

The UIStackView is a nonrendering subclass of UIView; that is, it does not provide any user interface of its own. Instead, it just manages the position and size of its arranged views. As a result, some properties (like backgroundColor) have no effect on the stack view. Similarly, you cannot override layerClass, drawRect:, or drawLayer:inContext:.
UIStackView 是 UIView 的非渲染型子类。它没有提供其自有的任何用户接口。相反的,它只管理被其管理的视图的位置和尺寸。因此,有些属性(如 backgroundColor)在 stack 视图上是无效的。类似的,你无法重写 layerClass,drawRect: 或 drawLayer:inContext: 方法。

There are a number of properties that define how the stack view lays out its content.
这里有一系列的属性来定义 stack 视图如何平铺其内容。

  1. The axis property determines the stack’s orientation, either vertically or horizontally.
    axis(轴向) 属性决定了 stack 的朝向,只有垂直或水平;

  2. The distribution property determines the layout of the arranged views along the stack’s axis.
    distribution(分布) 属性决定了其管理的视图在沿着其轴向上的布局;

  3. The alignment property determines the layout of the arranged views perpendicular to the stack’s axis.
    alignment(对齐) 属性决定了其管理的视图在垂直于其轴向上的布局;

  4. The spacing property determines the minimum spacing between arranged views.
    spacing(空隙) 属性决定了其管理的视图间的最小间隙;

  5. The baselineRelativeArrangement property determines whether the vertical spacing between views is measured from the baselines.
    baselineRelativeArrangement 属性决定了其视图间的垂直间隙是否根据基线测量得到;

  6. The layoutMarginsRelativeArrangement property determines whether the stack view lays out its arranged views relative to its layout margins.
    layoutMarginsRelativeArrangement 属性决定了 stack 视图平铺其管理的视图时是否要参照它的布局边距

Typically, you use a single stack view to lay out a small number of items. You can build more complex view hierarchies by nesting stack views inside other stack views. For example, Figure 5 shows a vertical stack view containing two horizontal stack views. Each of the horizontal stack view contains a label and a text field.
通常情况下,你会使用一个 stack 视图来布局小数量的视图。你可以通过在其他 stack 视图中嵌套多个 stack 视图的方式创建更加复杂的视图层次结构。举例,Figure 5展示了一个包含两个水平 stack 视图的垂直 stack 视图。每一个水平 stack 视图各包含一个标签和一个文本框。

Figure 5.Stack 视图的嵌套Nested stack views

视图的嵌套
You can also fine tune an arranged view’s appearance by adding additional constraints to the arranged view. For example, you can use constraints to set a minimum or maximum height or width for the view. Or you can define an aspect ratio for the view. The stack view uses these constraints when laying out its content. For example, in Figure 4 the image view has an aspect ratio constraint that enforces a constant aspect ratio as the image is resized.
你也可以通过增加被管理的视图的额外约束来完美的调节一个被管理视图的展现。举例说明,你可以使用约束类设置视图的最小或最大的高度或宽度。或者你可以定义一个长宽比。当平铺其内容时,stack 视图将使用这些约束。举例来说,在Figure 4中,当图片被缩放时,图片视图的一个长宽比约束被强行赋予了一个长宽比常数。
Be careful to avoid introducing conflicts when adding constraints to views inside a stack view. As a general rule of thumb, if a view’s size defaults back to its intrinsic content size for a given dimension, you can safely add a constraint for that dimension.
注意:当给一个 stack 视图内的视图增加约束时要特别注意避免传入冲突。作为惯例,如果一个视图的尺寸在一个指定的维度上默认回到其原本内容尺寸,那么你可以安全的在这个维度上增加约束。

维护其管理的视图与子视图之间的统一性Maintaining Consistency Between the Arranged Views and Subviews

The stack view ensures that its arrangedSubviews property is always a subset of its subviews property. Specifically, the stack view enforces the following rules:
Stack 视图确保它的 arrangedSubviews 属性将一直是其 subviews 属性的子集合。明确的说,stack 视图强制实施了以下规定:

When the stack view adds a view to its arrangedSubviews array, it also add that view as a subview, if it isn’t already.
无论何时 stack 视图增加了一个视图到它的 arrangedSubviews 数组,其也将把这个视图作为子视图增加,如果还未增加的话。

When a subview is removed from the stack view, the stack view also removes it from the arrangedSubviews array.
无论何时一个子视图从 stack 视图中被移除,那么 stack 视图也将将其从 arrangedSubviews 数组中移除。
Removing a view from the arrangedSubviews array does not remove it as a subview. The stack view no longer manages the view’s size and position, but the view is still part of the view hierarchy, and is rendered on screen if it is visible.
从 arrangedSubviews 移除一个视图并不会将其作为子视图移除。stack 视图将不再管理该视图的尺寸和位置,但是该视图仍将是视图结构的一部分,并且当其可见的情况下仍会被渲染到屏幕上。

Although the arrangedSubviews array always contains a subset of the subviews array, the order of these arrays remain independent.
当 arrangedSubviews 数组一直包含着 subviews 数组的子集合,这些数组间的顺序仍然是独立的。

The order of the arrangedSubviews array defines the order in which views appear in the stack. For horizontal stacks, the views are laid out in reading order, with the lower index views appearing before the higher index views. In English, for example, the views are laid out in order from left to right. For vertical stacks, the views are laid out from top to bottom, with the lower index views above the higher index views.
arrangedSubviews 数组的顺序定义了展现在 stack 中的视图的顺序。对于水平 stack 视图,这些视图将以阅读顺序平铺,即较小索引的视图在较大索引视图的左侧。对于垂直 stack 视图,这些视图是从上到下平铺的,及较小索引的视图在较大索引视图的上方。

The order of the subviews array defines the Z-order of the subviews. If the views overlap, subviews with a lower index appear behind subviews with a higher index.
subviews 数组中的顺序定义了子视图在Z轴上是顺序。如果视图重叠,有较小索引的子视图将出现在有较大索引的子视图后方。

最近才开始往github上放东西 在公司写的又不能放= = 大家姑且看看吧

github地址: https://github.com/FuThD

1 0