title: How can I create UIStackView with variable spacing between views?
body: <p>I have a simple horizontal <code>UIStackView</code> with several UIViews stacked inside.  My goal is to create variable spacing between views.  I am well aware that I can create constant space between the subviews using "spacing" property.  However my goal is to create variable space.  Please note, if at all possible, I would like to avoid using invisible views that act as spacers.</p>
  
  <p>The best I came up with was to wrap my <code>UIViews</code> in a separate <code>UIStackView</code>, and use <code>layoutMarginsRelativeArrangement = YES</code> to respect layout margins of my inner stack.  I was hoping I could do something similar with any <code>UIView</code> without resorting to this ugly work-around.  Here is my sample code:</p>
  
  <pre><code>// Create stack view
  UIStackView *stackView = [[UIStackView alloc] init];
  stackView.translatesAutoresizingMaskIntoConstraints = NO;
  stackView.axis = UILayoutConstraintAxisHorizontal;
  stackView.alignment = UIStackViewAlignmentCenter;
  stackView.layoutMarginsRelativeArrangement = YES;
  
  // Create subview
  UIView *view1 = [[UIView alloc] init];
  view1.translatesAutoresizingMaskIntoConstraints = NO;
  // ... Add Auto Layout constraints for height / width
  // ...
  // I was hoping the layoutMargins would be respected, but they are not
  view1.layoutMargins = UIEdgeInsetsMake(0, 25, 0, 0);
  
  // ... Create more subviews
  // UIView view2 = [[UIView alloc] init];
  // ...
  
  // Stack the subviews
  [stackView addArrangedSubview:view1];
  [stackView addArrangedSubview:view2];
  </code></pre>
  
  <p>The result is a stack with views right next to each other with spacing:</p>
  
  <p><a href="https://i.stack.imgur.com/ZEmuU.png" rel="noreferrer"><img src="https://i.stack.imgur.com/ZEmuU.png" alt="enter image description here"></a></p>