Autosizing superview height according to tallest subview button - ios

I have following layout. The yellow view is a buttonsContainerView which acts as a container for 3 equal width buttons that it holds. All the three buttons are subview of yellow coloured buttonsContainerView. All the constraint that I have given are basic and visible and understandable in screenshot but still I will explain them below.
buttonsContainerView - (yellow view) Pinned to top of viewController's view's safeArea with an inset of 20. It's leading and trailing are pinned to view's leading and trailing.
Button 1 - Top, bottom and leading pinned to buttonContainerView's top, bottom and leading respectively. Trailing pinned to Button 2's leading.
Button 2 - Top, bottom pinned to buttonsContainerView's top and bottom respectively. Leading pinned to Button 1 trailing. Trailing pinned to Button 3's leading.
Button 3 - Top, bottom and trailing pinned to buttonsContainerView's top, bottom and trailing respectively. Leading pinned to Button 2's trailing.
Equal widths - All three buttons are given equal width constraint with each other.
Problem - Any of these three buttons may get a longer title dynamically and according to various phone screen sizes it may not accommodate in single line. I want my buttonsContainerView's height to resize its height according to the tallest button, i.e., button with longest title hence greater height (In screenshot - Button 2, the centre button). In my case, buttonsContainerView is taking up height of smallest button (Button 1 and Button 2 in screenshot). How do I achieve this using AutoLayouts?

I have recreated your setup and can't find any problem with it, here is the picture below, can you show more info about this buttons setup? or maybe try to remove them and add again:

You can achieve this by using combination of top insets constraints by next algorithm:
1. Add to buttons horizontal constraints (left, right, same width).
2. Add a center vertically constraints for all buttons.
3. Add top inset constraints with low priority for all buttons.
4. Add greater or equal top insets for all buttons with hight priority.
Main idea is in low priority constraints for vertical insets of the buttons, the autolayout engine will try to satisfy it, but the greater or equal top insets will prevent to do it for small buttons.

Related

Avoid button height expanding due to other button's height expansion in cell

I have a custom XIB in which I have two buttons which I have pinned to the left and right edges and have added constrains to top, left and bottom for leftButton(named First Button) and top, right and bottom for rightButton(named Second Button)
Now my problem arises when I add padding to the right button(Second Button) only.
Since the left one have top and bottom constraints and adding padding to the right increases the contentSize of the view , so does the leftButton size as well
I checked the height of both the buttons inside the "Debug View Hierarchy" and both are different (left one being smaller and right one being bigger)
What I want to achieve : Increase the height of the rightButton(Second Button) but keep the height of the leftButton(First Button) as is
What I have tried so far :
For leftButton( First Button )
I tried changing the "Content Hugging Vertical" priority but it does not makes a difference
Reduced the top and bottom constraint's priority from 1000 to 999 so that maybe the height (which is smaller as showing in the debug view hierarchy) will be given the preference
EDIT
Leading margin >=10 failure
Try this:
Left Button
constrain Leading to superview
constrain CenterY to superview
constrain Top and Bottom to >= desired value
Right Button
constrain Trailing to superview
constrain CenterY to superview
constrain Top and Bottom to >= desired value
EDIT
To explain your follow-up question...
Changing leftButton leading from = 10 to >= 10 does not satisfy auto-layout:
Do you want it to be 10? 11? 20? Auto-layout only sees that you want it >= 10.
If you add a constraint between the buttons:
Now we're telling auto-layout: keep 60-pts between the buttons, and at least 10-pts leading for the left button.
When the right button title gets a little longer:
Auto-layout "pushes" the left button to the left, keeping 60-pts between them.
If we run out of room:
Auto-layout still keeps 60-pts between the buttons - by moving left button to the left - but maintains at least (>=) 10-pts leading.
As you see, though, this will require one of the buttons to compress... so you'll need to give one button a higher Content Compression Resistance Priority than the other so auto-layout knows which one you want to compress.

Fill Vertical Space of UIScrollView

I'm working on a layout that has a UIScrollView, and what I want to do is have a label pinned to the top of the scrollable area, and a button pinned to the bottom, with a minimum amount of space between the two.
The idea is that if the label grows in height the button will be pushed downwards so that the user has to scroll down to get to it. However, if the label is short, the space between it and the button should grow so that the button stays at the bottom of the scrollable area.
Below is a rough mockup of what I mean:
NOTE: The second shot is after scrolling all the way to the bottom of the UIScrollView, I want the button to be hidden when scrolled to the top; i.e- simply placing the button below the scroll view is not an option.
You can achieve the desired result with autolayout only, without having to change constraints via code. The steps are as follows:
1) Create your scrollview and pin it to all sides:
2) Add a view, which will act as content view, to the scrollview and pin it as well:
3) Your label and button will be added to this contentView, but before we can do this, we must add 2 additional constraints to the contentView. Create constraints for the width and height of the contentView to be equal as the SafeArea. You should set the priority of the equal heights constraint to low (250):
4) Add your label and button inside the contentView. The label pinned to the top and the button pinned to the bottom;
5) Lastly, add a vertical spacing constraint between the label and the button. Set the desired minimum amount of space between the button and the label as this constraint's constant, for instance 8, and set the relation to be Greater Than or Equal:
With this, if the label grows in height, the button will be pushed downwards so that the user has to scroll down to get to it.

Make button fill a percentage of a view

Inside of a viewcontroller, I have a view that is the entire height of the viewcontroller, it is 150px wide, and its all the way to the left of the viewcontroller. I have 2 buttons inside the view that are the full 150px wide all of the time, but I want them to stack on top of each other, and each take up 50% of the height all of the time, no matter which device they are on. What constraints do I need to put on each of the buttons to make them do this, all in storyboard? Thank you!
Set up the top button so that leading, trailing, top, and height are all equal to the superview. Set up the bottom button so that leading, trailing, bottom, and height are all equal to the superview.
Then edit the two height constraints and set the multipliers to 0.5.

UIScrollView and Autolayout prevent contentview compression

In IB I have a view controller that contain a scrollview.
The red view is inside the scrollview
The minimum height of the red view is 504px, so on iphone 4 it should scroll and on iphone >=5 it should extend and layout the buttons to fill the blank.
I set the constraints of the red view to 0 from top, leading, trailing and bottom of the scrollview and also centered horizontally and vertically.
My redview has a minimum height of 504
The blue and green views have equal height
Everything is fine for iphone >=5 but for iphone 4 the red view is compressed to be the size of the scroll view and the buttons are touching each others.
After multiple constraints adjustments I'm wondering if it's even possible to do what I want 100% in IB with autolayout
You say (emphasis mine:)
"I set the constraints of the red view to 0 from top, leading, trailing and bottom of the scrollview and also centered horizontally and vertically. My redview has a minimum height of 504"
First, I can see some redundancy (the bold part).Your redView does not need the vertical constraint since you have already pinned it on the edges of the scrollview and has a defined minimum height.
But this could or not be the cause of the buttons coming closer together, depending on the rest of the constraints.
Have you set vertical space constraints for the buttons?

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