How to give dynamic height to labels contained withing stackview in swift - ios

Inside a UItableview Cell I have following structure.
It has two Stackviews Left and Right
1) Left Stackview contains 3 views. Each view containing 1 label (Label1,Label2,Label3)
2) Right Stackview contains 2 views. Each view containing 1 label (Label5,Label6)
Constraints for Label1,Label2 and Label3 are Top,Left,Right and Bottom with respect to it's parent views.
Now when I add text to Label1 , Label2 and Label3 at run time it gives dynamic height but leaves space in top and bottom, for those label having less text. Below is the output.
I want to reduce the top and bottom spaces for all the three labels i.e Label1,Label2,Label3.
Any help will be appreciated.

This may help you
I think You have use distribution of Stack view as fill equally so it gives equal space to each one.
I think for this you have to use fill proportionally. this will adjust height accordingly
Choose fill proportionally from here
if this not working then choose fill proportionally and give height to each label it will adjust.
Check how to give height to labels .
after giving each label height inside stack view like this .
your it will adjust itself according to data.Like this

For dynamic height use self sizing cells in tableview. Set rowHeight and estimatedHeight properties for tableView
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 100
Set the stackview distribution property to .fillproportionally
as per the UIStackView documentation of .fillproportionally property
A layout where the stack view resizes its arranged views so that they
fill the available space along the stack view’s axis. Views are
resized proportionally based on their intrinsic content size along the
stack view’s axis.
in code:
stackView.distribution = fillproportionally
or in storyboard

Please check the attached screenshot, there are showing fully step by step guide line. How to set UILabel dynamically height inside vertical stackview with multiple UILabel's.
If you have any queries please comment.
Check the attached screenshot

Related

Proper Alignment and Distribution selection for Horizontal StackView

Following is the screenshot showing the design I want to implement.
For this, I am using a vertical stackview that encompasses four other horizontal stackviews. Then each horizontal stackview includes an image and a label as indicated below.
I set the alignment of the horizontal stackviews fill and tried changing the Distribution. However, I am not able to get the desired result.
What alignment/distribution combination could generate the desired effect?
Or should I eliminate horizontal stackviews and populate views inside the vertical stackview?
You can accomplish this with a tableView , but if you have a few items then
setting a spacing for the main stackview and every inner stackView should give the needed padding
for Full look check the demo
https://github.com/ShKhan9/stackV
Follow the steps,
1. Add a vertical stackView with top, leading and trailing constraints to the view.
Alignment - Fill
Distribution - Fill
Spacing - 20
2. Add 1 horizontal stackView to the vertical stackView
Alignment - Fill
Distribution - Fill
Spacing - 20
3. Add imageView with height and width constraints set to 30 in the above horizontal stackView.
4. Duplicate the horizontal stackView in vertical stackView the number of times as per your requirement.

How to use auto layout to resize views in a table view cell?

I have a cell in which I place four buttons and four labels. Each button gets assigned a picture with width 50 and height 50. Furthermore, all buttons have a corresponding label describing what they're intended for.
My objective is to have the buttons and labels resize to keep the buttons' and labels' aspect ration intact while the screen dimension changes on different devices. I have been playing with auto layout changing the hugging and compression to achieve this but haven't been successful yet. Any help would be much appreciated...
I think you should take a look at a UIStackView, because this seems exactly as a use case for stack. Just put each pair button/label in a stack, and then all four pairs into a horizontal stack, which you constraint to the cell itself. You should be able to handle all you need just by configuring the stack’s properties (axis, distribution, alignment, spacing).
Embed your button and label into a view. Set the width of this view equal widths to content view and change the multiplier value to 1:4. This will adjust the widths of the views according to superview. Also, set the top and bottom constraint to 0 for this view.
Provide center align y-axis constraint to button after setting the width and height constraint to 50. Set its top constraint to a value you deem fit.
Set labels's leading and trailing constraint to a value like 8. Choose center alignment for text. Also, provide top constraint to buttona nd bottom to its superview.
Copy the view and paste to create the three views and provide them equal widths constraint to the first view. Also, provide their leading, trailing, top and bottom constraints.
Here are a fast tutorial in how to achieve that:
1-
2- completion of the first Gif:
Note you can achieve the same output using a UIStackView

Constraints are not working at Buttons with Images Xcode

today i have set up some constraints in a view but there looks awful. Which Constraints have i to add (see screenshots)
In XCode
In Simulator
If I have understood your requirement right, you need to have four images with labels below these individual images. If this is the case, you can use collection view whose cell contains a image view and a label. You can give the spacing between cells, size of cell and intercell spacing through delegates. Also just you need to specify image view and label constraints in collection view cell. Like for instance, image view placed horizontally centre and pinned to top of superview, label placed horizontally pinned to bottom of image view and top of label to give a gap between them.
Fix the all images height and width, and given the leading, trailing, top and vertical spacing between label and images. Also fix the label height. You will given the min 4 and max 6 constraints for every fields.

UIStackView : Change UIStackView image width using storyboard

The issue : Image take most of the stack and text shrinks
I tried to change content hugging priority but no changes !
How to do it in UIStackview using storyboard?
What I want the cell to look like Eventbright event Cell?
What I got is an image that take most of the cell?
Instead of Fill, change the Distribution of the UIStackView to Fill Proportionally and add constraints for the width of each element. Those constraints will serve as a guide to proportions on one element to each other and not as fixed sizes.
I tried it the following way.
The overall set up is given in pic:
At run time result is as given:
First Stack view is fill proportionally
imageview width is 1:4 of stackview width (control and drag from imageview to stackview and set equal width to 25% )
From control and drag select equal width. as shown
stack view 2 is fill equally
stack view 3 is fill equally
Hope it can help

Dynamic table view height

The idea is to resize height of the cell automatically based on few controls heights. As you can see on image above I have Top Label and Bottom Label. These two labels can have different height based on text length.
Few words about setup in storyboard.
I set number of lines to 0 for 2 described labels to allow grow their height dynamically based on given text.
For the Top Label I have next constraints:
For the Bottom Label I have next constraints:
So I we say about vertical spacing between 1000 green label and bottom label it's every time the same:
But without this spacing cell won't stretch height. How can I reduce this vertical spacing? Because there is to much spacing between "1000 green label" and bottom label in case if top label have big height because of text.
In -viewDidLoad method I set:
[self.theTableView setEstimatedRowHeight:145];
[self.theTableView setRowHeight:UITableViewAutomaticDimension];
Seems it works pretty cool sometimes with a bug described here but I don't know how to restrict that vertical spacing:
http://www.appcoda.com/self-sizing-cells/
Set the bottom label's top constraint to be >= 11.5 (or whatever its minimum spacing should be).
This will let the cell adjust that vertical spacing, depending on the other content in the cell.
Update:
In iOS 9, this would much more simply be handled by UIStackView.
A horizontal stack view would constrain (and determine the cell height based on the) two inner vertical stack views. The left vertical stack would handle the image, banner, and label layout, and the right vertical stack would handle the top label, 10000, and bottom label layout. You'd only need with 4 constraints (for the horizontal stackView to constrain it to the contentView).

Resources