(0067)iOS开发之iOS新增类UILayoutGuide的用途


UILayoutGuide  是iOS 9苹果新增的API。(苹果文档地址:https://developer.apple.com/documentation/uikit/uilayoutguide?language=objc

A rectangular area that can interact with Auto Layout.

译文:它是一个可以与Auto Layout 之间互通的一个矩形区域。


苹果文档的概述:

在你定义的使用user interface 中用UILayoutGuide来代替您为了达到view的间距或者封装而创建的虚拟视图(你为了布局创建的无用的view hiden =yes)。通常,有很多Auto Layout情况需要( 虚拟视图)dummy views。dummy views是一个空视图,它没有自己的任何视觉元素,只在视图层次结构中定义矩形区域。只是为了占位。例如,如果您想使用约束来定义视图之间empty space的大小或位置,则需要使用dummy view来表示该空间。如果您想要居中一组对象,您需要一个dummy view来包含这组视图。类似地,可以使用dummy view来包含和封装用户接口的一部分。dummy view允许您将大型复杂的用户界面分解成独立的模块化块。如果正确使用,它们可以大大简化您的自动布局约束逻辑。

dummy view也有很多缺点,在视图层次结构中添加dummy view有很多成本。首先,创建和维护视图本身需要付出代价。第二,dummy view是视图层次结构的完整成员,这意味着它为层次结构执行的每一项任务增加了开销。最糟糕的是,不可见的dummy view可以拦截针对其他视图的消息,从而导致很难找到的问题。

因此,UILayoutGuide 就是为解决dummy view缺点设计执行,但在一个更安全更有效的方式做它。布局指南不定义新视图。他们不参与视图层次结构。相反,它们只是在拥有视图的坐标系统中定义一个矩形区域,可以与自动布局交互。

Creating Layout Guides

To create a layout guide, you must perform the following steps:

  1. Instantiate a new layout guide.

  2. Add the layout guide to a view by calling the view’s addLayoutGuide: method.

  3. Define the position and size of the layout guide using Auto Layout.

You can use these guides to define the space between elements in your layout. The following example shows layout guides used to define an equal spacing between a series of views.


ILayoutGuide *space1 = [[UILayoutGuide alloc] init];

[self.view addLayoutGuide:space1]; 

UILayoutGuide *space2 = [[UILayoutGuide alloc] init];

[self.view addLayoutGuide:space2]; 


[space1.widthAnchor constraintEqualToAnchor:space2.widthAnchor].active = YES;

[self.saveButton.trailingAnchor constraintEqualToAnchor:space1.leadingAnchor].active = YES;

[self.cancelButton.leadingAnchor constraintEqualToAnchor:space1.trailingAnchor].active = YES;

[self.cancelButton.trailingAnchor constraintEqualToAnchor:space2.leadingAnchor].active = YES;

[self.clearButton.leadingAnchor constraintEqualToAnchor:space2.trailingAnchor].active = YES;


Layout guides can also act as a black box, containing a number of other views and controls. This lets you encapsulate part of your view, breaking your layout into modular chunks.


UILayoutGuide *container = [[UILayoutGuide alloc] init];

[self.view addLayoutGuide:container]; 


// Layout the contents of the container

[self.label.lastBaselineAnchor constraintEqualToAnchor:self.textField.lastBaselineAnchor].active = YES;

[self.label.leadingAnchor constraintEqualToAnchor:container.leadingAnchor].active = YES;

[self.textField.leadingAnchor constraintEqualToAnchor:self.label.trailingAnchor constant:8.0].active = YES;[self.textField.trailingAnchor constraintEqualToAnchor:container.trailingAnchor].active = YES;

[self.textField.topAnchor constraintEqualToAnchor:container.topAnchor].active = YES;

[self.textField.bottomAnchor constraintEqualToAnchor:container.bottomAnchor].active = YES


// Set exterior constraints.UILayoutGuide *margins = self.view.layoutMarginsGuide; 


[container.leadingAnchor constraintEqualToAnchor:margins.leadingAnchor].active = YES;

[container.trailingAnchor constraintEqualToAnchor:margins.trailingAnchor].active = YES;

[container.topAncor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor constant:20.0].active = YES;


Note:

Layout guides 提供了一种轻量级方法来封装布局的一部分。注意,这种技术只会影响自动布局与封装视图的交互方式。它不以任何方式的视图层次结构的变化。然而,这不是创建模块化用户界面的唯一方法。容器视图和容器视图控制器提供了更大程度的封装,让您将布局、视图层次结构甚至相关的视图控制器代码分隔开来。有关更多信息,请参见iOS的视图控制器编程指南中的适应性和大小变化。
此外,布局约束不能完全封装它们的内容。系统仍然将布局指南中的可选约束的优先级与指南之外的可选约束的优先级进行比较。


demo:https://github.com/muyushifang07/UILayoutGuideTest/tree/master





















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值