iOS UITableViewCell dynamic sized tableview cell with imageview and label - ios

In my app i have a UILabel and a UIImageView under each other. Both of them have a fixed width, but their height supposed to change.
I want the UILabel's height to change to fit the text it's holding and i want the UIImageView's height to change automatically to fill the width and keep it's aspect ratio.
My problem is that autolayout doesn't allow me to have both item's vertical content compression resistance priority to the same constant. Because of this one item will always oppress the other.
What i expect is the tableviewcell to change it's height dynamically to fit its content.
I do not want to modify anything from code.

You can do it in alot possible way like
1.) Using StackView here is a link to help http://www.raywenderlich.com/114552/uistackview-tutorial-introducing-stack-views
2.) You can select your UILabel and its superView than select aspectRatio , in a same way select your UIImageView and the superView than select aspectRatio.
It will help Thanks.

Related

iOS - Make UIImageView resize to fit UILabel with AutoLayout

I'm having this issue since quite some time now and it's driving me crazy: I have a UILabel with text that can be dynamically changed, and I have a UIImageView that should act as the text's background. I would like the UIImageView to resize itself to always fit the UILabel's text.
"Paid" could become "Free", or "Payant" in French for instance, and the box behind should always resize.
Using Storyboard, I have tried adding a UIImageView with the UILabel on top of it, and setting the constraints of the UILabel to the UIImageView, but this ends up resizing the UILabel to the UIImageView's size. I cannot set a fixed width or height for the UIImageView because it should adapt.
My second try was to make a UIView, recategorize it as a UIIimageView, add the UILabel as a subview, make the UIView resize to the UILabel and add the image programatically but once again, no success.
What is the right way to do this?
For the first method, I assume you set the had the label and the image view have the same vertical and horizontal centers as well has the same height and width. The problem is the layout engine is assume the intrinsic size of the image is more important than the label. To change that you need to change hugging priority and the compression resistance of the views. Go to the size inspect of the label (picture) and increase the hugging priority to required. This will for it's height and width to be it's intrinsic height and width. You could also decrease the image view's compression resistance, so it is more amendable to being sized down.
The constraints for the label:
You can change the top and leading to place it in the appropriate spot in the view.
The constraints for the image view:

Don't specify width & height constraints on my UIView (like UILabel can do)

If I put a UILabel in a view and specify a vertical space constraint and an horizontal space constraint, it's fine.
If I do the same with my custom UIView, IB will complain because it'll lack an height and a width constraints.
I guess it comes from the fact that UILabel can expand both vertically and horizontally. But so does my custom UIView, so how can I inform that my view has such rights?
The default behaviour for a UILabel is "Fit To Content"(Editor-> Fit To Content). This means that UILabel will grow as the Content increases for a given font size. For a view to position itself, it needs X, Y, Width and Height. Width and Height are given by the text it contains. Rest two properties you have already provided. I hope that answers your question.

How to set this kind of constraints: UIImageView's width based on UILabel's Length in AutoLayout?

I already set UIImageView.width(Green Color Background) == UILabel.width + 20 in my tableView cell ,but when the label's content changing , the label 's height is changing at first ,but I want the label to grow wider first , if it can't be more wider then grow higher. what 's the problem here
Hi yes you can do it. I am explaining below how can u do it with auto layout.
At the time of applying constraint, instead of giving hight and width
to UIImageView just try to give it three constraint with UILabel which
are " Equal Width, Equal Height and Aspect Ratio"
For Applying these three constraints with UILabel the shortcut is just
drag and drop your mouse cursor from UIImage to UIlabel with right
click only, so you can see above three options. After applying it ,give
UIlabel Leading and trailing constraint( or whatever u want) and most important thing, set
your UIlabel to size to content fit. For UILabel,shortcut is just
click on UILabel and click on top toolbar's tab EDITOR and you will
find option for setting UILABEL as size to content fit so after
setting it UILabel will automatically set its frame as per its content
and so as UIImageView.

Working with AutoLayout using UITableViewCell

I am working with autoLayout, I want to place a UIlabel on UITableViewCell, which should be always at the right of the cell and the center of right.
So here is what I want to achieve
So here you can see label named RC which I am talking about.
So here my cell height changes dynamically, so whatever be the text height I always want to be the center right.
2) Now as the cell height depends on the text of the label sentence. I want to make sure that if the cell has the default height and it will not decrease below it. It can increase to any height.
Select your label go to editor then select vertical center in container, also fix it using the trailing space from super view and set fit size to content and it should work. For dynamic tableViewCell height created using autolayout find the TUTORIAL. This will help you a lot.

Resize UITextView inside a UITableViewCell with iOS Autolayout

My Goal
I'm creating a custom UITableViewCell (MessageCell), and I'd wish to have my UITextView resizing both in width and height whenever the cell changes its size, while keeping some basic constraint like margins to the edges.
Note : I also change my cell's height in the heightForRowAtIndexPath method depending on the content which will be placed into UITextView.
What I tried
Here are all the constraints currently applied & the cell :
I tried :
With a UILabel.
To fix a Vertical Space from the bottom, thinking it would change the height to fit all the constraints.
To override Height with an User Constraint Height >= 43 (my initial height), but the purple-automatic Height is re-created again whenever I do this.
To find a solution on SO first, but didn't find a case like (even if the UITextView's height's resize with autolayout seems to be a frequent issue).
Plenty of combinations of various & random constraints until my fingers bleds.
How it render now
So...
If someone has any clue or guideline to achieve my goal, I'd appreciate!
I might add, I'm totally new to Autolayout & constraints. My reference : Ray Wenderlich's awesomeness
You need to get rid of that height constraint that the text view has. Make sure you have constraints to something above and below the text view (usually the top and bottom of the cell), and then you should be able to delete that height constraint.

Resources