Using Autolayout it's really common to use dummy/spacing views to achieve a specific Layout.
Dummy views come in help when you want to add a constraint relationship between empty spaces.
It's totally fine to use dummy/spacing views but when they are too many on the hierarchy the rendering process
may slow down. UILayoutGuide
comes in help because it allows you to set constraints avoiding the view overhead.
We as Developers would really appreciate Apple will make UILayoutGuide
available inside Interface Builder but currently
we can use them only programmatically.
Because it's not possible to place UILayoutGuide directly from Interface Builder as Xcode out of the box feature,
LayoutGuideView
allows you to place UILayoutGuide
as any dummy/spacing view. The dummy view will be replaced
at runtime using a carbon copy UILayoutGuide
moving all the dummy/spacing view constraints to the UILayoutGuide
.
LayoutGuideView
works as a IB transient view that will be automatically translated into a UILayoutGuide
at runtime.
Once the LayoutGuideView
is dispatched from IB and added to it's superview it starts the migration process. It automatically install the UILayoutGuide
into its superview and starts migrating the constraints it's involved in. At the end of the constraints migration process the LayoutGuideView
is then removed from it's superview to let it die in peace ⚰️
- Add the following to your Podfile:
pod 'LayoutGuideView'
-
Integrate your dependencies using frameworks: add use_frameworks! to your Podfile.
-
Run pod install.
- Add the following to your Cartfile:
github "falcon283/LayoutGuideView"
- Run carthage update and follow the steps as described in Carthage's README.
Just place a View that represent your layout guide in IB and layout it as your needs using AutoLayut Constraints. Don't forget to change the class type to LayoutGuideView
into the Identity Inspector. Optionally you can set an Layout Guide name in the Attributes Inspector to identify the generated UILayoutGuide
in the list of layoutGuides
of your superview.
There are only few simple rules to respect
- Do not use
LayoutGuideView
as arrangedSubview ofUIStackView
. fatalError will be raised. - Do not embed any subview into
LayoutGuideView
. fatalError will be raised. - Do not allocate
LayoutGuideView
programmatically ... why you should do it? fatalError will be raised.
- Why I can't use it in
UIStackView
?
Simply because UIStackView
is a non rendering view that have its own Layout Rules as per user configuration. If you introduce other layout constraints between arrangedSubviews
the StackView can't works as expected and the result of your layout will be unpredictable. By the way you can always create a Layout Guide inside a view that is contained in the arrangedSubviews
.
- How can I use it as Dummy Layout Container if I can't embed subviews?
Well, Dummy Views are often used as Layout Container to place a certain area somewhere and then constraint the inner subview as we want. You can't do it with LayoutGuideView
because it will vanish at some point and you will loose your subviews. What you can do is consider it as a "reference layout area" ... maybe "Safe Area" just come to your mind ;)
If you keep your "wanted" subview at the same hierarchy level of LayoutGuideView
you can constraint it to the LayoutGuideView
as usual with just different constraints.
Please take a look to the Example App.
- I could use also Relative Margins, what are the benefits of using
LayoutGuideView
in this case?
While Relative Margins are perfectly fine and you should keep using it in most cases, they have some limitations.
The layoutMargins
or directionalLayoutMargins
in iOS 11 are staticaly defined and can't react the layout changes unless you change it by code.
Using LayoutGuideView
instead, in case you need, you can define the margins in terms of AutoLayout. Think about the safe area ... it changes the "margins" automatically when you rotate your iPhone X horizontally. Using LayoutGuideView
you can do the same because you can now define in terms of AutoLayout how the "empty space" is constrained.