Scale vertically stacked buttons with Auto Layout - ios

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:

Related

Autosizing superview height according to tallest subview button

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.

Image view autolayout to pin 60pt from horizontal edges, but auto scale height?

I'm trying to figure out an approach to the following:
I have a square image, I want to align it horizontally and vertically in the centre of the screen and constrain it in a way where its left and right edges are 60pt from screen edges.
I got thus far, but am now getting an error from XCode about top and bottom image constraints. How can I make it so image keeps its square ratio and auto resizes based on screen size?
Example, images width became 294pt in screenshot below, yet height remained unchanged, where as it should change to 294 pt as well.
You need to add 3 more constraints.
Vertically align your view to superview
Horizontally align your view to superview
Set aspect ratio of your view to 1:1 (Important)
On the whole you should have 5 constraints. The screenshot is given below.
this error occurs when auto layout did not find enough information about a component on the storyboard. as you did it here you only provide the trailing and leading constraint so now it asks for height and width.
if you want the same height on every device selects the height constraints that will fix the heigh 300.
Or simply you can do this set the alignment constraint to Center vertically and horizontally and then add top and leading constraints

How to implement constraints for 2 labels between 2 images for xib file

Edit: PLEASE LEAVE A COMMENT IF YOU'RE GOING TO DISLIKE SO I CAN IMPROVE MY QUESTION
I'm trying to recreate a custom table view cell in my xib file as shown below. The company's square image is on the left. The company's name and company's booth (2 UI Labels) are to the right of the company's image. The star button is to the right of the text and is a square image. I guesstimated that the company's image and favorites button should be about 8px from the top and edge.
I tried to create 4 constraints for the top, bottom, left, and right of every element (image, 2 UI labels, and button). I also added 1:1 aspect ratio constraint to the image and button to make sure the image would be square. Then I aligned the left edge of the 2 UI labels. I vertically centered the image and the button. However, it came out with no star button and the location and title switched. How do I create this design using constraints?
Their is no difficulty with that.
First if we talk about your left UIImageView, Set following constraints,
Leading constraint
Fixed Height
Fixed Width
Centre Vertically
After that the UIImageView on left, set following constraints,
Trailing space from superview
Fixed Height
Fixed Width
Centre Vertically
Now for both Labels, put them in a UIView and give that UIView following constraints,
Leading space from left image view.
trailing space from right image view.
top space from superview
bottom space from superview
Now for upper UILabel, Set following constraints,
Leading space
Trailing space
top space
Now for lower UILabel, Set following constraints,
Leading space
Trailing space
top space from upper UILabel
bottom space
After all this, i think that this will work for you.
You can use the constraints in the image below. It will work for all screen size and for any height of row.

UILabel implemente autoheight with center placement and a button to the right using AutoLayout

I want to have a UILabel with a button to it's right centered horizontally like of the picture.
I've implemented an "Align by Horizontal center" for the UILabel and standard space between label and button and it usually works just fine.
The problem appears when UILabel's text is very long and it goes out of bounds and the button is not even visible.
I've tried to set lines count for the UILabel to 0 and set questionLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.questionLabel.frame)what makes the label to grow vertically. But it's width is very small (it's the base width from ui designer) so it looks like a column of text.
So the question is how to make UILabel to:
to fill all available horizontal space without hiding the button
autoscale vertically if there is no enough horizontal space to fit text into one horizontal line
be centered horizontally
It's missing the constraint from the trailing of the UIImageView to the trailing of your container view or margin. I used a Greater Than or Equal constraint so if the label has a short length, the image will stay close to the label - using the horizontal space constraint from the label to the image.
With that, your label - that is centered relative to your view, will be centered and will respect your trailing constraint of the UIImageView to the view using all the horizontal space that is possible to use as follow:
In the following image it's possible to see that the label reaches the maximum width it can, according to the constraints:
you could remove the width constraint and add a horizontal space constraint from the right side of your label to your image and one from the left side of your label to the left border of your view (set the constant to the minY Value of your Image to get it centred)

iOS Auto Layout >> View is not Changing its Size

I have a design for a screen that should look like this (other things will be added later, but I cannot seem to resolve the basis...):
I have added Constraints to determine the following:
Both Labels are Constraint in spacing to the screen edges.
Middle View is Horizontally and Vertically Constraint to the Middle of the Background View Center.
I have added 4 Constraints to express Minimum and Maximum Vertical Spacing between the Middle View and the Labels (Current spacing as Maximum and Standard spacing as Minimum).
I have also added 2 Constraints to the Middle View to define Spacings from the Screen right and left edges.
I thought that it should be enough, but in reality, when switching between Retina 3.5 and 4 the Bottom Label disappears and the Middle View is cut in the middle:
I have tried lowering the Middle View Content Hugging and Content Compression Priorities, and still no good.
Here are the Warnings I get:
Any idea how to resolve this?
Or alternatively, how to approach it differently (preferably, still using Auto Layout)?
Add Equal Width & Equal Height constraints as well & It will work
Add TopSpaceToContainer constraint for Top Label. Then add width and height constraints for your yellow view at the middle. Remove the multiple vertical spacing constraints given to the Top Label and Bottom Label.

Resources