Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override spacing for a UIStackView

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).

like image 331
Ben Packard Avatar asked Mar 26 '16 18:03

Ben Packard


1 Answers

From iOS 11 it is possible to use:

stackView.setCustomSpacing(spacing: CGFloat, after: UIView)
like image 195
barola_mes Avatar answered Sep 23 '22 00:09

barola_mes