Adjust UIImageView to heigth of UILabel - ios

I have an UIImageView and an UILabel next to each other with horizontal spacing constraint 0 and vertical alignment constraint. Now I want the image to be 80% as high as the label, so I set an equal height constraint with multiplier 0.8. But now instead of the image shrinking the image and the label grow insanely big. The constraint is still applied since the image is smaller than the label, but they are just way to big. My guess is that the constraint resizes the label based on the image instead of the other way around. So how can I tell it to do what I want?

Ary you trying to achieve like I shown in following image?
Where UIImageView view will be having 80% height of your UILabel?
If yes then Height Constraint will be key here.
Select both UI elements and apply Equal Heights constraint
It will make both as same height.
Then select the Height Constraint of UIImageView and add
Multiplier = 0.8 in Size inspector
Apply One by One constraints to each element first to show in expected way then add Equal Heights constraint. It will show conflict because other constraints are already present, then apply Multiplier = 0.8 and finally you are good to remove the unwanted constraints i.e. Fixed Height.
To avoid auto resizing of UIImageView according to Image, you need to have fix Height constraint for UILabel.

Related

Simple row layout using Autolayout constraints

I've a simple layout that I know how to do it with a horizontal stackview. But for learning purpouses I want to know if it is posible to code using AutoLayout constraints.
The layout is simple, it's a rectangular row that contains an image and a label.
The problem is that the label can have multiple lines and be bigger than the icon.
The desired layout is that image and label had to be centered in Y axis and the bigger height one have a margin of 8 points at top/bottom.
You should give the image and the label a margin constraint for both top and bottom. Use greater or equal as relation for these margin constraints.
Give a top and bottom margin constraint for image and label. Set high priority with relation Greater Than or Equal.
First Write this in viewDidLoad
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 44.0;
Set numberOfLine property to 0 in StoryBoard
Set UIImageView Constrains like:
Fixed Height and Width and Vertical Center in Container.
Set UILable Constrains to HorozontalSpacing from UIImageView Trailing Top Bottom from SuperView i.e Cell.
Hope it will work...

Autolayout Height >= 0 Frame in Interface Builder

I'm using interface builder to set up a table view cell with two labels. One of them should have dynamic height, depending on how much text it holds. (The cell is also of dynamic height based on the height of that label.) I have the constraints working properly – both the label and the cell resize correctly based on input text. My problem is that to make it work I had to set a constraint on the resizing label of height >= 0. For some reason, IB forces me to make the on-screen height of the label 0, which makes it hard to see/work with. The little dots below "Label" is the 0-height label:
If I set it at a height greater than 0 (which should be allowed by the constraint) the constraint turns red in error:
I'm curious why IB won't let me set something with a >= constraint on its height at a height greater than the minimum value. Is there a way I can get rid of this error?
While working with label text you don't require to give the height to UILabel. As it will automatically take the height based on font size. All you need to do is give vertical,top and bottom constraint to both the labels and remove the default text from attributes inspector. Now you can create table cell with dynamic height.
Top Label constraints:
leading,Trailing,Top
Bottom Label constraints:
leading,Trailing,Bottom
and also give vertical spacing between top and bottom label.
And with the self sizing cell height you can get you desired result.
ref url: self-sizing-cells.
Here is the image of constraints which i've set:
Here is the image of constraints for both the label.
And the sample code which i've added to display data with result.
Hope it helps in solving your problem.

Choosing maximum of two widths for auto layout

I have a UIImageView and two two UILabel as its child. Now using auto layouts I want that UIImageView's width should be equal to the maximum of these two labels so that both of these are visible completely. Is there a way to define this constraint?
Instead of using same width constraint (which is fine too if you specify <= constraints, but needs some extra work after) you may use leading and trailing constraints. If your imageview will have a >= leading and <= trailing constraints for both of the labels + your labels will be in their intrinsic size, then imageview will have the maximum width. This can be illustrated with the picture below:
Make sure your labels has horizontal compression resistance set to required (1000) to prevent labels from being down-resized.
You can define your constraints in the code itself and add a constraint after you know the size of the wider UILabel
Take a look at this answer on how to add constraints programmatically
https://stackoverflow.com/a/13055845

How to simulate conditional hugging priorities in AutoLayout?

I have a UIView that contains a multiline UILabel and a UIImageView. The imageView is a square that is a specific height and width and is centered vertically in the cell, while the label is constrained to the top of the view. Here's a little illustration:
I want to be able to make the parent view expandable based on either the UILabel or the UIImageView, based on which one has the bigger height. How would I go about doing this (in Storyboard)?
You would set a bottom and top constraint for both the label and image view at a high priority. Set the constant equal to the amount of padding you'd want.
Then, on the parent view, you'll want to add a height constraint with a lower priority than the top/bottom constraints of the label and image.
That way, you guarantee padding between the label/image and their superview, which will force the superview to expand its height since its priority is lower than that of the padding.

IOS Label and View constraints height

I am really confused about constraints is iOS, i've read a lot of articles, but get stuck, when tried to set Height constraints of UIView according to UILabel content height. I know that is the common question, but i really don't understand the solutions. I thought that the main thing in this question is the constraint priority, but i can't set them properly. In one case, the height of UIView wont change to 0, and in other UILabel height does not make any sense.
So. I've got:
UIView with height constraint, and descendants constraints (below)
UIImageView with height=32, width=32, top=10, left=10, bottom>=10
UILabel with left to UIImageView = 10, top=10, right=10, bottom=10
And i want:
If i got any text to place in the UILabel, i want to dynamic height of UIView according to height of content size of UILabel.
And if there is no text to place in UILabel, i want to set the height of UIView equals to 0 (hide the UIView totally).
UIImageView - is just the icon. If there is no text, must be shrink to 0, if there is some text, must have height 32, top constraint 10, bottom constraint more or equals to 10.
UPD:
Fix the problem, by adding height constraint programmatically. (don't like this)
You can make the constraints take full control of the label,image and view heights, if you set the priorities correctelly.
Set the four vertical margins priorities to less than 1000 (lets say 999)
Set the image height priority to 999 too.
Set lable vertical hugging priority to 1000
Set image height less than or equal label height with 1000 priority
Set label height less than or equal container view height with multiplier 1: 10(or any big number) with priority 1000
Like this
The View is this
and this

Resources