问题
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)
.
回答1:
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.
回答2:
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
.
回答3:
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:
- UILayoutGuide – Auto Layout’s Invisible Helpers
- UILayoutGuide.h
来源:https://stackoverflow.com/questions/19536992/what-is-uilayoutguide