Vertical align elements evenly in autolayout - interface-builder

In Interface Builder, I have five UISwitch elements. I want to lay to have the same spacing between the elements, when I lay them out horizontally.
How do I set the autolayout so that they'll be evenly spaced, but without filler views?
I've tried setting the left and right margin constraints, but that resulted in conflicts about the horizontal hugging. As you can see in the Simulator screenshot, that is my current situation with the equal margins after AutoLayout asked me to act on the horizontal hugging.

You want to use the new iOS9 class UIStackView

Related

Set the width of a view to 50% of its container in the nib

I am creating a view with four table views in it. I want each table view to take 50% of the height and width of the containing view and to auto-resize when the window is resized.
I have been unable to find a way to do this thus far.
You should use autolayout in storyboard. Resizing and stuff like that becomes very easy that way. Basically you want to set constraints for each tableview to the edges which they are touching and their neighboring tableviews (distance: 0). Furthermore you have to set constraints for equal width and equal heights.

How to center and reduce the width of buttons inside a UIStackView

In a vertical UIStackView I have stacked four UIButtons. The UIStackView is centered (vertically and horizontally), the left side corresponds to the left edge of the device and the right side to the right. Inside the stackview, the buttons take all the horizontal space the stackview takes, so the stackview and its content share their width.
Now, I want to reduce the width of my buttons, so that they are 80% the width of the container (on any device except for iPad) and I want to center them too.
How can I do? I need a solution either in code (Swift) and using any possible property I can't find in XCode
Instead of pinning the stack view to both sides of its superview, give it a width constraint that causes it to be 80% of the width of its superview.
The best working solution is
button.widthAnchor.constraint(equalToConstant:self.stackview.frame-30).isActive = true
This works perfectly fine.

Using autolayout, how can I center a subview in an NSSplitView without forcing the width NSSplitView to be static?

I have an NSSplitView with content in both NSViews. The left NSView has 2 constraints – Equal Widths and Equal Heights. The right NSView has something simple, say an NSTextField, which is centered via constraints Center X Alignment and Center Y Alighment. This is what I hoped it would look like as I resize the window and/or the NSSplitView divider:
This is what's happening:
I've tried a great deal of configuration changes, I've tried using an NSSplitViewController vs just dropping an NSSplitView into an NSViewController to adjust more parameters programmatically, but I'm not having any luck. Whenever resizing the window, the left view always takes over the excess space. The same happens with the divider (it can be resized, but letting go of the mouse button causes it to snap right back). It seems there's something fundamental that I'm missing here.
The text field's content hugging priority is probably higher than the split view item's holding priority. Fix that and the view should probably work the way you expect.
Also, if, when you resize the view, the left view is resizing with the window while the right view stays the same size, then that suggests that the left view's holding priority may be higher than the right's. You should make the side that you want to stay the same size have the higher holding priority.
That said, I'm not sure what you mean about the constraints you've set on the subviews. "The left NSView has 2 constraints – Equal Widths and Equal Heights." What do you mean here? Its width is equal to what? Its height is equal to what? Do you mean it has an aspect ratio constraint? Frankly, I can't think of what constraints of those kinds would make sense for a view within a split view.

UIImageView: use all available space via auto layout

I'm trying to use Auto Layout to minimize the gap between a UIImageView (in green) and a UICollectionView (in red). In the simulator (which I assume is showing an iPhone 4-size screen) the two views line up nicely, with only a small gap between:
However on my actual device, an iPhone 5 with larger screen, there's a big gap:
How can I specify auto layout constraints so that the UIImageView (green box) takes up as much space as available, regardless of screen size, while keeping the UICollectionView pinned to the bottom toolbar?
I've tried experimenting with adjusting the constraints, adding pins, etc in XCode without much success. I feel like I'm just flailing around not really understanding how they work, and I have yet to find a good tutorial that explains how do something like "expand to take up as much vertical space as possible".
So you need to specify that the gap between the two views is fixed, and that the height of the bottom view is also fixed. In visual format language that would be
"V:|[topView]-[bottomView(==150)]|"
V: means this is a vertical constraint
|[topView] means pin the top of topView as close to the top edge of the superview as possible.
- means that the space between the two views is fixed
[bottomView(==150)] means that bottomView must be 150 pixels
]| means that the bottom edge of bottomView is pinned to the bottom edge of the superview
You could also add these constraints in Interface Builder by setting a fixed height on the bottom view, and a fixed gap between the top and bottom views

How to have a view resize using Xcode's auto layout

I am using Xcode's auto layout feature for the first time in a project where I have several NSPopUpButtons.
Now what I want to achieve is to have two popUpButtons in a row together with their labels and when the window is resized I want both popUpButtons to adjust their width while keeping the horizontal spacing between each other.
However no matter how I apply the constraints I just don't get the popUpButtons to change their size with the window. They will always break their horizontal spacing constraints and just increase/decrease the spacing to the labels. I hope it gets a bit clearer what I have done from this screenshot:
I have set the spacings between the labels and the popUpButtons to fixed values with 1000 priority and have set the width constraints fo the popUpButtons to be greater or equal to the initial size.
How must I set my constraints to have the popUpButtons resize?
While writing this question I realized what the trick is:
In the size inspector of the NSPopUpButton I had to reduce the Content Hugging Priority.
Obviously this controls how closely the view wants to 'hug' its content. So when the hugging priority is higher than the resize priority the view will not want to increase its size because that would mean to have more empty space between its bounds and its content.
Then in my special case, I could also pin both NSPopUpButtons to have the same width and voilà: the popUpButtons will perfectly resize while keeping the spacing constant.

Resources