It’s time to get to know UILayoutGuide

UILayoutGuide is a virtual placeholder object added to iOS 9 to help developers use the auto layout layout.

First, a layout in a scene may be met: there are two width buttons, two buttons in the middle of the gap with them to a distance equal to the left or right edge. What do you do with auto layout?

It's time to get to know UILayoutGuide

constraints have equal constraints, but there are no margin equal constraints, and obviously we can’t directly meet requirements through configuration constraints.

A very common idea is to change margins into a placeholder View, so that you can set equal attributes. However, this View should be set to hidden and should not be visible to the user. In this way, we can set these three virtual View widths equal, and then set their leading and trailing constraints in order to achieve the desired result.

There is also a situation where two controls require two groups and center. In this case, we will also introduce a View that adds the two controls to the container View and sets the center of the container View.

But in order to assist the layout of the View, there is a painful place: it is in the hierarchy of View and will receive messages when the event is routed. That’s what UILayoutGuide wants to solve. UILayoutGuide can be added to View and, like View, various constraints can be set to participate in the layout. However, it will not be rendered and will not respond to event routing. You can also include some View directly like box.

The following directly with the code shows how to set the two controls in the center. The Snapkit is used as a constraint, without using native crappy API.

First, we initialize two View and one LayoutGuide.

Let label = UILabel (label.text) = "UILabel" view.addSubview (label) let (testView = UIView) testView.backgroundColor = UIColor.cyan view.addSubview (testView) let = UILayoutGuide (container) view.addLayoutGuide (container)

LayoutGuide is added to View using the addLayoutGuide method.
then we set the edge of label, testView, and container, and there is 20 offset between them.

{label.snp.makeConstraints (make) in make.left.equalTo (container) make.centerY.equalTo (container)} {testView.snp.makeConstraints (make) in make.left.equalTo (label.snp.right).Offset (20) make.height.equalTo (40) make.width.equalTo (120) make.right.equalTo (container) make.centerY.equalTo (container)}

In this way, the width of container is equal to the width of two view plus 20 offset. The two view centerY and container agree. We then set the container constraint on it:

Container.snp.makeConstraints {(make) in make.height.equalTo (5) (view)}

You can do it by running:

It's time to get to know UILayoutGuide

Anything that requires a virtual View help can be given to UILayoutGuide. It is lighter, faster and more efficient. UILayoutGuide does not really create a View, but creates a rectangular space that participates only when auto layout is involved.

Finally, let’s remind you that this is a new feature of iOS 9.
God bless those who are still supporting iOS 7.

Welcome to my micro-blog: @ no tales of Zhuo classmates