What is _UILayoutGuide?
Asked Answered
P

3

13

I need to know about _UILayoutGuide, like what it is, what it does and why it present in the hierarchy of UIView as a subview with almost always frame = (0,0,0,0).

Paulpaula answered 23/10, 2013 at 8:46 Comment(0)
D
6

UILayoutGuide is normally referred to -topLayoutGuide and -bottomLayoutGuide, those are not really constraints, but they are view elements conform to a protocol called UILayoutSupport.

You can find more info about that protocol here. The value is often 0 but you should pay a lot of attention where you ask their size.

Disendow answered 23/10, 2013 at 8:56 Comment(4)
Ok this is included for autoresizing support. I think we have nothing to do with this Object, does Apple using it internally ? Actually I am having some subview calculation and want to exclude these two objects. Is there any way (excluding hidden property) to exclude these two objects in that calculation of UIView's Subviews and are these objects always be 2 in count?Paulpaula
Personally I would not remove them from hierarchy. You can can loop through view subviews doing class checking using the instance method -isKindOfClass:Disendow
I'm not removing those object from hierarchy but just not considering them in some calculation. As far as 'isKindOfClass' concern it won't work because we don't have _UILayoutGuide class. it might be a private class use by apple.Paulpaula
conformsToProtocol:@protocol(UILayoutSupport) if not and you haven't any other around they are simply UIViewDisendow
O
21

This is a private Apple class, which is used for topLayoutGuide and bottomLayoutGuide when auto layout is enabled. If your navigation bar is opaque, one of these "views" will be in [0,0]. If your navigation bars are translucent, same view will usually be in [0,64] in portrait (20pt for the status bar + 44pt for the navigation bar). There is an analogous one for the bottom toolbar, if you have one.

The reason it is done this way is so you could define layout constraints, which work with UIView objects.

One thing to notice, if you have some logic which works on subviews, be careful not to include them in your calculations. You can ignore these by testing:

[subview conformsToProtocol:@protocol(UILayoutSupport)]


On iOS 9, there is a new private class, _UILayoutSpacer, which is not a descendant of UIView, but can be used to set up constraints. The system seems to work in a dual mode, where controllers loaded from xibs and storyboard still use _UILayoutGuide, while controllers created in code are set up using _UILayoutSpacer.

Outhe answered 23/10, 2013 at 8:59 Comment(6)
your guess is absolutely on mark, I am having some subview calculation and want to exclude these two objects. Is there any way (excluding hidden property) to exclude these two objects in that calculation of UIView's Subviews and are these objects always be 2 in count?Paulpaula
See edit how to test and ignore. I think there are always two in the current implementation.Kunstlied
An update for 2015 readers: UILayoutGuide is now an official part of the iOS 9 and OS X 10.11 APIs. According to WWDC 2015 session 219 ("Mysteries of Auto Layout, part 2"), UILayoutGuide is a lightweight object which simply represents a rectangle for the layout engine. Constraints can be added to each UILayoutGuide instance, just like a UIView. It is the preferred way to "pad" out or otherwise lay-out multiple objects instead of using hidden UIViews to do the job. They also expose the layoutMarginsGuide property, which simply represents the rectangle within the margins.Dichogamy
@Dichogamy UILayoutGuide is still a protocol in iOS 9, leaving the internal implementation, still, behind private classes.Kunstlied
As of iOS 11, topLayoutGuide and bottomLayoutGuide are deprecated in favor of the new safeAreaLayoutGuide which is just a plain old UILayoutGuide (i.e. not a protocol wrapper to a private class).Oxeyed
@DavidJames This question is very old—before layout guides existed at all. Top and bottom layout guides were faked by adding a fake view.Kunstlied
D
6

UILayoutGuide is normally referred to -topLayoutGuide and -bottomLayoutGuide, those are not really constraints, but they are view elements conform to a protocol called UILayoutSupport.

You can find more info about that protocol here. The value is often 0 but you should pay a lot of attention where you ask their size.

Disendow answered 23/10, 2013 at 8:56 Comment(4)
Ok this is included for autoresizing support. I think we have nothing to do with this Object, does Apple using it internally ? Actually I am having some subview calculation and want to exclude these two objects. Is there any way (excluding hidden property) to exclude these two objects in that calculation of UIView's Subviews and are these objects always be 2 in count?Paulpaula
Personally I would not remove them from hierarchy. You can can loop through view subviews doing class checking using the instance method -isKindOfClass:Disendow
I'm not removing those object from hierarchy but just not considering them in some calculation. As far as 'isKindOfClass' concern it won't work because we don't have _UILayoutGuide class. it might be a private class use by apple.Paulpaula
conformsToProtocol:@protocol(UILayoutSupport) if not and you haven't any other around they are simply UIViewDisendow
W
4

UILayoutGuides

UILayoutGuides represent a rectangle in the layout engine. They will not show up in the view hierarchy, but may be used as items in an NSLayoutConstraint.

iOS 9

In iOS 9 Apple provides a new improved way of controlling negative space called a UILayoutGuide. A UILayoutGuide, or layout guide, is an empty rectangle in a layout against which constraints can be applied to define the it’s relationship to other UILayoutGuides or UIViews.

UILayoutGuides don’t have a hierarchy and they are not part of the view hierarchy. They are owned by a UIView, but they cannot own a UIView or another UILayoutGuide. UILayoutGuides do not contain a CALayer and they are not a UIResponder. This means that there is no drawing overhead added to to the render phase by adding a UILayoutGuide and there is no impact on the UIEvent handling process by having a UILayoutGuide.

Source:

Wildebeest answered 6/11, 2015 at 15:50 Comment(2)
Thank you Rafat for sharing this blog. it's really so informative.Paulpaula
so long story short in the old ages you had to add dummy views to create constrain to 'aFewPointsBeforeYourEdges'. topLayoutGuide.bottomAnchor represents the bottom of the dummy view at the top of the screen and its best to constrain your subviews to this rather than using view.topAnchorTurbosupercharger

© 2022 - 2024 — McMap. All rights reserved.