UIStackView with multiple label - uistackview

Is it possible to create UIStackview without fixed height constraint.
I have two label. Height of both label are dynamic. UIStackview should expand if labels expand. How to do that ?

For vertical UIStackView, add your UILabels and make their
vertical content hugging priority to 1000
Now your UIStackView height will depend upon you labels height.

Related

Table view cell with label and textField and equal space between each other

i want to create a table view with cells which have a textField.
What drives me crazy is the spacing between label and textField cause i want a equal spacing in all cells.
To better explain take a look at this screenshot.
So im searching for a good way to get the spacing between Second Label and Second textField and set this on the other cells, too.
Is there a way to do it in the storyboard and not by edit the constraint in code?
I would suggest that:
use a horizontal stackview to hold the label and textField with some spacing in between, like 10.
set the label's width constraint to a fixed value.
the details are as follows:
add a horizontal stackview into the cell's contentView
set the stackview constraints to fill the contentView with some margins:
eg. leading: 20, trailing 20, top 10, bottom 10.
add a label and a textField into the stackview.
in the stackview's Attribute inspector, set the spacing to 10.
set the label's width constraint to 100, or 0.3:1 to the contentView's width
so the label will have a fixed width, fixed spacing between label and textField, and the textField will take all the horizontal space remain.

Label height won't change dynamically

I have a cell with a few labels init. One of the label is for a question. My app has different questions with different lengths. So I need my label to have a dynamical height. I want to achieve this with auto layout. When I run my app with my current auto layout it doesn't change the height (See pictures) . I am not sure what I am doing wrong. I have changed the number of lines of the label to 0 and I have added:
tableView.estimatedRowHeight = tableView.rowHeight
tableView.rowHeight = UITableView.automaticDimension
to my viewDidLoad(), but it won't change the height of my label.
Constraints:
Label on device:
Autor label Constraints:
Bannerview Constraints:
Set the Label height as Greater than or equal to zero in constraints. If give like this height of the label get increased based on the content.

Random table view cell height is not working perfectly

I am designing a chat app in which I am using the UITableViewAutomaticDimension property of table view when I am setting the label lines to 6 then all cells height is according to the text which label contains but when I increase the number of lines for label then some cells have extra height. Even when scrolling it then cell height again changed.You can check the image so that you can understand it easily thanks.
You need to give label constraints from top and bottom, rather than center vertically.
Give vertical content hugging priority

Constraints for labels with changing size

Pic. 1
Pic. 2
Pic.1 - it is a structure of my ViewController. Label1 and Label2 have changing size (content comes from backend) for different users. If content of labels is small, I need to set Top Space constraint for TableView (vertical spacing) to Button (pic.1), but if content of labels is big, set Top Space constraint for TableView (vertical spacing) to Stack (pic.2). How to set constraints for TableView?
Embed your image,button two labels and stack in a view.
Then add following constraints
Top, leading and trailing to new container view
vertical space b/w container and table.
Required constraints for your image,button two labels and stack
Don't add bottom constraint for button.
Add bottom spacing to stack to its superview(embedded container) with a >=.
Height to container view with minimum height and set priority to low(250)

Dynamically sizing UITableViewCells

I have worked with dynamically sizing UITableViewCell before. But currently I am facing a problem statement where I am unable to achieve dynamic sizing using autolayout
My cell has three components viz-a-viz one UIImageView, two UILabel
The structure of the cell is as follows
UIImageView has to be of fixed width and height in my case 60X60
UIImageView will be vertically centered inside the UITableViewCell
UIImageView is having a leading constraint of 15 from the UITableViewCell
Now the two UILabels are vertically placed
Both the labels can have dynamic height and I want them to push expand the cell using autolayout but I am only able to achieve keeping the top UILabel of constant height and the below UILabel increases in height.
I want both the UILabel to expand vertically and increase the size of UITableViewCell.
Just use StackView for both label and make both labels numberOfLine to 0 and line break should be WordWrap. give leading,trailing,top and bottom constraints to stack view. for spacing between labels give spacing for stackview on xcode detail pannel.
See the Screenshots for the setting of the constrains.
Add a height constrain to the label with the constant greaterthanEqualTo a fixed value (see screenshot 2). and set the lines to 0. In the table view heightForRowAt indexPath set that to UITableViewAutomaticDimension .