How to make one stack view compression priority less than other - ios

I'm trying to make one stack view not collapse the labels in it, but setting the stack view compression and hugging priority is not giving any results.
The stacks are placed in a collection view cell. In the title stack there are 3 stacks with fill distribution and in the info stack there are 3 stacks with the same distribution.
I've pinned the title stack on top, bottom, and leading to superview, and trailing to the leading of info stack. And pinned info stack top, bottom, and trailing to superview.
I tried to set content & hugging priority that way that info stack won't collapse, as follows
[infoStack setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[infoStack setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
[titleStack setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
[infoStack setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];

Related

StackView item not aligning to the left

I want my both "bubbles" be aligned to the left.
They are in a stackView with the symbol and the mailLabel.
For some reason shorter text always aligns right.
I tried all distribution settings in the stackView already.
For future reference ... when you post a "layout" image, it is very helpful to expand the complete hierarchy, including all of the constraints.
I'm assuming your horizontal stack view's Trailing is constrained to the Trailing of the Content View. That tells auto-layout to keep the stack view the *full width of the content view.
To keep your "bubbles" left-aligned in the Horizontal Stack View, set Distribution: Fill, then constrain its Trailing to be less than or equal to the Content View's Trailing.
Assuming you used the "standard" way of setting constraints in IB - where the Content View Trailing is constrained to the Stack View Trailing, it's "reversed" and should be greater than or equal.
So, your constraints probably look very close to this:
and should be changed to this:

With equal heights constrain between two views, how do I decide which view shrinks/inflates?

Lets say I have a horizontal stack view, wherein 2 stack views are present (each internal stack view naturally represents a column). Every internal stackview has 2 labels. Now I set the equal with constraint and assert that the first constraint in the first internal stackview needs to have equal height to the first view in the second stack view. How do I decide, whether the first shrinks, or the second grows?
in this case the green view grew. How do I make the yellow view shrink?
You can decide whether first view in the first stack view should shrink or the first view in the second stack view grows by setting Content Hugging Priority and Content Compression Resistance Priority for both.
If you want view in the first stack view shrinks then set Vertical Content Compression Resistance Priority for the first view in first stack view to be less than Vertical Content Hugging Priority of the first view in second stack view and vice versa for the opposite scenario.
For example set Vertical Content Compression Resistance Priority for the first view in first stack view to be 250 and Vertical Content Hugging Priority of the first view in second stack view should be 251. In this case view in first stack view will get shrink.

Autosizing superview height according to tallest subview button

I have following layout. The yellow view is a buttonsContainerView which acts as a container for 3 equal width buttons that it holds. All the three buttons are subview of yellow coloured buttonsContainerView. All the constraint that I have given are basic and visible and understandable in screenshot but still I will explain them below.
buttonsContainerView - (yellow view) Pinned to top of viewController's view's safeArea with an inset of 20. It's leading and trailing are pinned to view's leading and trailing.
Button 1 - Top, bottom and leading pinned to buttonContainerView's top, bottom and leading respectively. Trailing pinned to Button 2's leading.
Button 2 - Top, bottom pinned to buttonsContainerView's top and bottom respectively. Leading pinned to Button 1 trailing. Trailing pinned to Button 3's leading.
Button 3 - Top, bottom and trailing pinned to buttonsContainerView's top, bottom and trailing respectively. Leading pinned to Button 2's trailing.
Equal widths - All three buttons are given equal width constraint with each other.
Problem - Any of these three buttons may get a longer title dynamically and according to various phone screen sizes it may not accommodate in single line. I want my buttonsContainerView's height to resize its height according to the tallest button, i.e., button with longest title hence greater height (In screenshot - Button 2, the centre button). In my case, buttonsContainerView is taking up height of smallest button (Button 1 and Button 2 in screenshot). How do I achieve this using AutoLayouts?
I have recreated your setup and can't find any problem with it, here is the picture below, can you show more info about this buttons setup? or maybe try to remove them and add again:
You can achieve this by using combination of top insets constraints by next algorithm:
1. Add to buttons horizontal constraints (left, right, same width).
2. Add a center vertically constraints for all buttons.
3. Add top inset constraints with low priority for all buttons.
4. Add greater or equal top insets for all buttons with hight priority.
Main idea is in low priority constraints for vertical insets of the buttons, the autolayout engine will try to satisfy it, but the greater or equal top insets will prevent to do it for small buttons.

Dynamic height for a horizontal UIStackview

I have a horizontal StackView with distribution as "fill equally". It has 3 labels.
I want one of the labels to have dynamic height.
When I set the number of number of lines to 0 for that label, it ends up breaking the constraint the spacing constraint at runtime.
All these horizontal stackviews make a one vertical stackview.
I have tried setting lower vertical hugging priority and higher resistance priority to the multiline label
Also it behaves like this in the XIB
Before changing the number of lines
After changing the number of lines
You do not need to set Content Hugging and Compression resistance property to achieve that effect.
Horizontal Stack Views: Set Alignment "Fill" & Distribution "Fill Equally"
Vertical Stack View: Set Alignment "Fill" & Distribution "Fill"
Set Label Lines to 0.
Set Top, Bottom, Leading Constraint for Vertical Stack View.
Set all constraints for UIStackView.
Update the UIStackview height constraints constant value based on UILabel text height
Not Directly answering the question rather on how to control the dynamic height of UIStackView.
Vertical Stack View. Make it free of height like no bottom constraints or no height constraints.
Contents(like view) are fixed height.
Both Alignment & Distribution of UIStackView will "fill"
now show or hide the contents then Stactview height will dynamically change.

How to center a Label with an ImageView next to it in Swift?

I am trying to have a UIImageView next to a UILabel with dynamic content. Both within a StackView.
Currently I'm using the Fill Alignment and Distribution. Changing it to one of the other options, changes the size of the ImageView (fixed Constraints to 30x30).The StackView has left and right leading Constraints of 0.
This is my current:
The is what I want:
How do I need to set up the StackView to archive my goals? Help is very appreciated.
You can try using a vertical stack view with an embedded horizontal stack view.
The first stack view has a vertical axis. Its alignment is centre and the distribution is fill (the default). This stack view has the following constraints: Trailing to superview, leading to superview and top space to top layout guide.
The second stack view contains the image view and label. Its axis is horizontal. The fill alignment and distributions are set to their default values. The spacing is set to 10 (can be changed to suit your needs). This stack view has no constraints.
The image view has a height and width constraints of 30 respectively.
The label has no constraints.
Both the image view and label have default content hugging and compression values.
If the label has longer text then then it will appear like this:
Have you tried manipulating content hugging and content compression resistance priority ? I believe these property will let you manage the length distance between both the image view and textView, and the stack view won't let you dynamically expend the textView as you enter more and more data so I think manipulating the hugging and resistance properties can give you a solution close to the required.
This can be achieved by creating a container view for the image view and the text label and then centering the container view in the original parent view.

Resources