UIStackView layout issue with UILabel - uikit

I have two UILabel inside horizontal stack view with alignment top and distribution fill.
The problem is second label is taking too much space rather than its intrinsic content size.
I also had set content hugging priority of second label to .high but still it's taking too much space. Is there any missing do I need to implement or any ways to fix it.

I found a way to solve this. Set contentHugging priority to .required for Horizontal axis.

Related

XCode: Stack view and constraints

Why would I need contraints on elements that live inside my stack view? Is not the idea of the Stack View to stack elements automatically thus making the need of explicit measures to be added?
Well Said. Stackview get its height and width from content size.
Sometime we cannot say exactly how much content size a ui element has.
For example if we have an imageview inside stackview and we assign image at runtime. The stackview will grow with the imageview. The question is how much. Well it depends on the size of image. So there we can feel undesired results. So we fix stackview size using predefined constraints.
Also, Stackview need constraints for position. Where to position stackview inside view of viewcontroller. So we need constraints for positioning our stackview.

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.

Setting IKImageBrowserView to one long row

I've got a IKImageBrowserView in an NSScrollView and I want to lay it out so it only scrolls from left to right.
By default the IKImageBrowserView lays out the images in a grid and the width is being correctly constrained but it's always increasing the height to fit the number of rows of images it's containing.
My naive approach to this has been to try setting the IKImageBrowserView's and NSScrollView's frames so the height is the equivalent of 1 row of images, but this had no effect.
Can anyone recommend the correct way to approach to this?
No sooner do I ask the question and I find the answer:
[imageBrowser setContentResizingMask:NSViewWidthSizable];

Resources