-2

Now I am reading the official documentation about the UILayoutGuide. In short, the UILayoutGuide class replaces a dummy view, which can represent the space between views or can encapsulate user interface. But, I can't understand the meaning well.

First, about the space which the dummy view create, do we need this? We can define the space very easily by using AutoLayout. I don't understand the need of the dummy view.

Second, what does encapsulation mean? I don't grasp what they want to tell.

Could anyone help?

Kazuya Tomita
  • 667
  • 14
  • 34
  • "We can define the space very easily by using AutoLayout" No, we can't — not when it is the space itself that we want to equate to other spaces. See my answer below, and this question (of which yours is actually a duplicate): http://stackoverflow.com/questions/20864823/springs-in-auto-layout-distribute-views-evenly-with-constraints-in-xcode-5 – matt May 05 '17 at 14:25

3 Answers3

1

You don't need dummy views, when auto layout can handle your views correctly. But in some cases, for example, you have 4 buttons in a row und 3 buttons in a row under it, sometimes its hard for auto layout to set the constraints correctly. For that you can just use an empty "dummy view" for the first 4 buttons as parent view to align with and the same for the next 3 buttons.

So it is easier to set the correct constraints for only the 2 "dummy views" and then use the dummy views to constrain the buttons correctly inside those views.

Encapsulation means, when you want to rearrange the 4 buttons 20 pixels up, you just have to move the dummy view 20 pixels up, instead of moving the 4 buttons by hand individual.

So that means, you have a better overview, same like encapsulating code for reuse or readability

Retterdesdialogs
  • 3,180
  • 1
  • 21
  • 42
1

Traditionally, there were a number of Auto Layout techniques that required dummy views. A dummy view is an empty view that does not have any visual elements of its own and serves only to define a rectangular region in the view hierarchy. For example, if you wanted to use constraints to define the size or location of an empty space between views, you needed to use a dummy view to represent that space. If you wanted to center a group of objects, you needed a dummy view to contain those objects. Similarly, dummy views could be used to contain and encapsulate part of your user interface. Dummy views let you break up a large, complex user interface into self-contained, modular chunks. When used properly, they could greatly simplify your Auto Layout constraint logic.

There are a number of costs associated with adding dummy views to your view hierarchy. First, there is the cost of creating and maintaining the view itself. Second, the dummy view is a full member of the view hierarchy, which means that it adds overhead to every task the hierarchy performs. Worst of all, the invisible dummy view can intercept messages that are intended for other views, causing problems that are very difficult to find.

The UILayoutGuide class is designed to perform all the tasks previously performed by dummy views, but to do it in a safer, more efficient manner. Layout guides do not define a new view. They do not participate in the view hierarchy. Instead, they simply define a rectangular region in their owning view’s coordinate system that can interact with Auto Layout.

From your source

Community
  • 1
  • 1
shallowThought
  • 19,212
  • 9
  • 65
  • 112
0

See my answer here: https://stackoverflow.com/a/20865342/341994

As you can see from the discussion, the way to solve the problem of equal spacing of views under auto layout is to introduce invisible "spacer views", like this (the invisible spacer views are shown in black):

enter image description here

That's because you can't write a constraint saying that constraints must be equal; you can only write a constraint saying that views have equal constraints.

Those invisible spacer views are the dummy views. UILayoutGuide are an innovation that provide a lightweight alternative to dummy views: they do what the dummy views do, but without the overhead of actual views — as I explain my answer here: https://stackoverflow.com/a/31523872/341994 And, as I say there, this is exactly what UIStackView does for you, automatically: it constructs those UILayoutGuide objects, so that you don't have to.

Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Very nice answer, but I don't understand "That's because you can't write a constraint saying that constraints must be equal; you can only write a constraint saying that views have equal constraints". What do you mean? Could you explain? – Kazuya Tomita May 06 '17 at 11:46