I frequently find that a UIStackView
is perfect for a particular layout, except that I need some variable spacing between the arranged subviews. UIStackView
uses its spacing
property to arrange the views along its axis, but is there a way to override these? For example, I would love to be able to add a single manual constraint using the visual format to define the required gaps, and somehow de-prioritize those of the stack view.
I could forgo the UIStackView
entirely of course, but the stack's ability to give back space between hidden subviews is pretty painful to replicate manually (adding and removing constraints based on which views are hidden, for example). I also tried wrapping individual subviews in their own stack views so that I can use the stack's layoutMargins
to add a top-margin. But this then requires managing the wrapper-stack's own hidden state in addition to that of the subview it contains (surprisingly - to me - a UIStackView
whose arrangedSubviews
are all hidden is not automatically hidden itself, so does not yield its spacing and margins back to its own superview).
From iOS 11 it is possible to use:
stackView.setCustomSpacing(spacing: CGFloat, after: UIView)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With