Note that there are some explanatory texts on larger screens.

plurals
  1. POiOS Auto Layout: Equal Spaces to Fit Superviews Width
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/13075415/autolayout-even-spacing">Autolayout Even Spacing</a> </p> </blockquote> <p>I'm trying to create a scrollable bar with buttons (similar to a <code>UISegmentedControl</code>). The superview is an <code>UIScrollView</code>. As soon as the buttons don't fit into the scrollview, the scrollview should be scrollable. So far, almost everything works fine:</p> <p>With a lot of buttons (scrolled to the right):</p> <p><img src="https://i.stack.imgur.com/2sV9j.png" alt="Scrolled View"></p> <p>Not so many buttons:</p> <p><img src="https://i.stack.imgur.com/TE5ii.png" alt="View is not scrolling"></p> <p>Now, my goal is that if there is room for all buttons, they should be equally spread across the whole 320px view. <strong>How can I define constrains for the spaces in between the buttons?</strong></p> <p>Right now, I'm using the following constraints (self is a <code>UIScrollView</code>):</p> <pre><code>UIView *firstButton = self.buttons[0]; [self.buttonConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"|-(5)-[firstButton]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(firstButton)]]; UIView *lastButton = [self.buttons lastObject]; [self.buttonConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"[lastButton]-(5)-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(lastButton)]]; UIView *previousView = nil; for (UIView *view in self.buttons) { if (previousView) { [self.buttonConstraints addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"[previousView]-(5)-[view]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(previousView, view)]]; } previousView = view; } </code></pre> <p>If I change the type of the superview from <code>UIScrollView</code> to an <code>UIView</code>, I get the following result, still not what I want, but at least it looks for the constraint of the last button that ties it to the right edge (makes sense, that this doesn't happen for the scrollview, as the content size is automatically set):</p> <p><img src="https://i.stack.imgur.com/cgiMF.png" alt="UIView as the superview"></p> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload