UIView hug all subviews using Auto Layout - ios

In the above, the orange, blue and green views are subviews of the yellow view. I need the parent (yellow) view's borders to hug its subviews. The only movement that's occurred between the two images is the green subview moving up and to the right.
Because the bottom of the orange view is now lower than the bottom of the green view, the parent view hugs the orange view's bottom edge.
Similarly, because the green view's right edge is past the blue view's right edge, the parent view hugs the green view's right edge.
Is there a way to do this with Auto Layout constraints, or would I have to write custom code?

For the bottom edge, both the orange and green views require two vertical space constraints each. They are:
Vertical space constraint with relation set to "Greater Than or Equal" and constant set to 0.
Vertical space constraint with relation set to "Equal", constant set to 0 and a priority lower than the first constraint.
The first constraint ensures that no matter what, the space will be greater than or equal to zero. The second constraint says if possible, also have the space equal to exactly zero.
The above can then be replicated for the right edges of the blue and green views, using horizontal space constraints instead.

Related

How to make grow UIViews proportionally

I have view simmilar to this below:
Views stack
How can I make it possible to make height of first and last view bigger proportionally, when making yellow view height smaller? Can I do it by setting content hugging priority and content compression?
Now that I apparently understand your problem, I suggest the following:
The top of the violet view has to be set equal to the top of the superview.
The height of the violet view is still undefined.
The bottom of you violet view has to be set equal to the top of the label.
The label has a fixed height.
The bottom of the label has to be equal to the top of the yellow view.
The yellow view has initially a fixed height.
The bottom of the yellow view has to be set equal to the top of the button.
The height of the button is still undefined.
The bottom of the button has to be set equal to the bottom of the superview.
This leaves the heights of the violet view and the button undefined. But your requirement is that their heights are proportional.
This can be achieved be setting them first to be equal to each other. Then double click the constrains and change the multipliers, e.g. make the multiplier of the violet view larger, and the multiplier of the button smaller.
Then you have an unbroken chain of vertical constraints, and auto layout should work correctly if you assign a height of 0 to your yellow view.

Three proportional views autolayout

I have three views I want to divide in a superview.
Currently, when I set proportional heights, I get weird results when updating the multiplier in those constraints.
As you can see, there is weird space between the top and the first proportional view and the second.
I want no room in between.
Who can help me out?
Two things immediately
It would be simpler to set the proportional height in this case between each of the views and their superview. This may not be what you want but either way my example below would work with slight tweaking
You only need to set two of the views to have proportional height. The third view (the one in the middle) can just take up the remaining space in between them.
See the Red, Green and Blue views -
The red view hugs the top, left and right and has a proportional
height
The green view hugs the bottom, left and right and has a proportional
height
The blue view hugs the left and right and has vertical spacing (zero)
to the red view and vertical spacing (zero) to the green view.

Scale vertically stacked buttons with Auto Layout

I have 4 buttons with UILabels under them. It looks good on the iPhone 6, but I'm having trouble getting the buttons to stay in the same configuration with Auto Layout. I've tried constraining the Aspect Ratio and the distances to the edges, but the aspect ratio still gets distorted. I deleted all the constraints for now.
What approach should I take?
The key is to use equal height constrains (red = orange, orange = blue, blue = purple) in conjunction with vertical spacing zero (same way between red and orange)
Pin the labels to the bottom (the white views in my example).
The leading and trailing of the buttons are also pinned to the borders of the superview to define the horizontal size.
Please follow the below steps to get your requirement done.
Give Each Button to label "Vertical Spacing" & "Center-Y".
The first button to it's superView "Top Space to Top Layout Guide".
The last button to it's superView "Bottom Space to Bottom Layout Guide".
set Each button to it's superView "Center Vertically in container".
Fix Buttons-Labels; heights & widths.
To achieve this , you have to add constraints for - leading space, trailing space, top space and aspect ratio for each element.
Constraint for Button:
Constraint for Label:

NSLayoutConstraint equal height does not divide equally

In my app, i need to put some boxes and make sure first two of them divided equally.
Navigation bar + status bar holds 64 point.
Fixed height constraint of 60 point set to welcome box.
Fixed height constraint of 145 point set to brown box.
Tabbar holds 50 point.
Rest of the height which is 417 point should be divided into two.
Green and Red boxes has its scrollviews in themselves. Scrollviews has layout constraint of same height, top, bottom, leading and trailing constraint for that of its superview.
After layout is done, i inspected that red box has height of 208.66667 and green box has 208.3333.
But each scroll views have height of 208.6667. So the scroll view at green box becomes vertically scrollable because its height(contentSize height) is bigger than that of its superview.
You have some options.
Resize the content of the scroll views to be 208px and ignore the left-over partial pixel.
Disable scroll bars on the scroll views if you don't need them.
Add a pixel to one of the other areas so that the red and green views can be equal heights.
Size the views to the exact pixel dimension you want rather than just an "equal height" constraint.

AutoLayout UITableViewCell

I am working with autoLayout for UITableViewCell
So here is my xib and constraints
Here I set constraints such that tableView has dynamic height
Whenever I run on iphone5, it looks this way correctly which I want
But when I run the same thing on ipad it shows this way
So I am not understanding how to make the ipad version look same as iphone version, Not understanding which constraints I am missing.
When working with autolayout constraints, formulate what you want into sentences.
Example:
I want the yellow view to be pinned to the right.
I want the yellow view to be pinned to the top and bottom.
I want the yellow view to have width of 50.
I want my label to be pinned at the top and bottom.
I want my label to be pinned to the left.
I want my label to be pinned to my yellow view, with 10 pixels between them. (Thus growing in width along with the superview width).
And there you have all your constraints. Now you just have to add them one by one. Top, Bottom, Right to superview and Width constraint with a constant of 50 for the yellow view. Top, Bottom, Left to superview and Right to Yellow view with constant of 10 constraints.
You've pinned your yellow view to the left of the superview, so on bigger screens, it will grow to fulfill that constraint.
It looks like you pinned the left edge of the yellow view to the left edge of the table view cell with a 300pt offset. That means on the iPad, the yellow view is still 300pts offset from the left edge of the screen, and grows to fill the rest of the width available.
What you probably want to do instead is pin the right edge of the yellow view to the right edge of the table view cell with a 0pt offset, then also pin the yellow view's width to its desired size.

Resources