Using Autolayout to set scaling rules for objects - ios

As you can see from the grab, I have all my spacing sorted and am pinning the leading & trailing edges properly; I am stuck on getting my buttons to scale horizontally so the space in-between stays the same. The conflict is all related to the fact that I haven't set a fixed width for the button, but in this case I don't want it fixed.
How can I resolve this autolayout conflict? Does it need some kind of width constraint? That defines a minimum at least?

select both buttons
click add constraints menu
select equal widths
select buttons one by one and add other constraints till all lines turn to blue like image below.
Hope it helps.

Related

Swift - Why ScrollView not full screen?

I had inserted a ScrollView into UIViewController and dragged ScrollView to fill the space between the navigation bar and the RAM label below:
But when I run the app, the ScrollView does not fill the space:
Please help me! Thank you very much.
P/S: Sorry for my english is bad.
The scroll view is not covering up the whole thing because you are running the app on a much bigger phone. The simplest solution is to run the app on iPhone 5.
However, if you want to solve the problem on all sizes of iOS device, you need to add constraints.
Constraints are things that tells a view when and how much it should resize and where it should be positioned.
To add a constraint, just select the view you wish to add a constraint to and go to the bottom right corner. You will see 4 buttons:
The leftmost button is used to embedding views in stack views. This is a feature of iOS 9. If your deployment target is lower, just ignore it.
The second button to the left is for adding constraints related to alignment - where the edges of the views are, what its baseline is and where it is positioned in the X and Y axes:
The third button to the left is used to add constraint related to margins, width, height and how the width and height should change when it is asked to resize (keep the aspect ratio, for example):
The rightmost button is used to let Xcode decide what constraints you should add. And I think most of the times its choices are okay. Sometimes though, you still need to do some tweaking before it works.
"So... what constraints should I add?" you asked.
Well, I think I should teach you how to think when you want to add a constraint. This way, you can figure it out yourself in the future.
You should first let Xcode guess what constraints you want. Just click the rightmost button and click "Reset to Suggested Constraints". This can save a lot of work if Xcode can get it right. So remember to always do this first.
Then, run your app on various devices and see if the view's position, size, and alignment are as you expected. If it is not, you might have to add and/or remove some constraints.
For example, if you found that your view is always the same size on different devices, (that could be bad because it means that some content my go out of view on smaller devices) it's probably because Xcode added a width and/or height constraint to the view. You should delete that so that the view's width and/or height is not fixed.
You can find your view's constraints in the view hierarchy:
Just select the constraint and press delete.
Uncheck Adjust subview option and add
scrollview.view.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
You need to add constraints for your scrollView. Set the leading and trailing constraints to 0. Pin the height of your scrollView and also don't forget to set the top layout constraint. You can either pin the height or add bottom layout constraint to your page control.
Constraints are very important and its even more important to set it correctly. Check the Apple Documentation - Working with constraints in IB
Uncheck constrain to margins and add 0 every one of the four limits of spacing to nearest neighbour.
My guess (from the little information we have) is that you are creating a constraint from your scrollview to the top of your view with a value equal to the height of the navigation bar. Set the value of this constraint to 0.
Just set the 4 constraints to 0 to the area you need and then uncheck the "Content Layout Guides" checkbox in the constraints tab here. It will automatically adjust to the area you have specified.
The checkbox to uncheck

AutoLayout issue with ULabel

I have 2 labels attached at top with "zero" constraint. One label have font 12, other 15. I face the problem, that i have unnecessary space between text and cell border. Please take a look:
You can see that left space to top is less then second. How to fix that?
You may want to check if the second label has option "relative to margins" checked, just uncheck it.
If you just want the labels to be on the same y position, just delete the second label's top constraint and add a baseline constraint between two labels.
Evgeniy Kleban,
As per your question you have two labels and for both the labels you have attached the constraint as 0 :)
My suggestion assign top constraint as 0 to only one :) Lets assume you are okay with the placement of label y :) Then delete the top layout constraint on label A16 and instaed hold control and drag from label y to label A16 and when you leave a popup appears select center vertically :)
This will ensure both the labels base lines will be aligned properly :) Hence your problem is solved :)

iOS Autolayout: two buttons of equal width, side by side

I am currently having difficulty with AutoLayout. I am using interface builder and am trying to position two buttons of equal width side by side as illustrated in the following image.
From the following Preview image, my titleImage has been properly constrained and is displaying correctly however the buttons are not. I have experimented by aligning button 1 with the leading edge of titleImage, and button2 with the trailing edge of titleImage, however the width distribution between the two buttons becomes skewed at this point, as demonstrated below.
My objective is to understand what constraints are missing and should be applied to the two buttons to maintain equal widths regardless of device. If possible I would like to achieve this through interface builder as opposed to additional code.
Add the following constraints
Assign equal width from button1 to button2.
Assign horizontal spacing between both buttons.
Assign leading space from button1 to its superview.
Assign trailing space from button2 to its superview.
Assign top space to both the buttons.
Let me know if it works for you.
Follow Steps and Screenshots for easy solution
Step-1)
For Button 1 : Set Constraints: (1)Leading, (2)Top or Bottom asper your need, (3)Height
Step-2)
For Button 2 : Set Constraints: (1)Trailing,(2)Top or Bottom asper your need, (3)Height
Step-3)
Press Ctrl + Drag From Button 1 to Button 2
Select Horizontal Spacing
Step-4)
Select Both Button (using Command) and Add Constraints Equal Width
OUTPUT
Hope it Helps you :)
Stack layout in iOS9, will do the job really nice.
Add stack view to your view and configure as follow:
You can check my example - you can easy aim this, by using proportional constraint.
Also you can easy aim proportional multiple UIViews.
Please look attached example
https://dl.dropboxusercontent.com/u/48223929/TestViewController.xib
My solution is to
Put a small view in the middle of two buttons and make it centre(Horizontal centre in container and vertical centre in contener as 0).
Add height and width to the small view.
Add buttons the constraints and give horizontal space constraints to small view.
Give the small view background colour same as buttons or View's colour.
Note: See the Screenshot.

Same width for a set of labels using auto layout

I need to create a simple layout with a few labels and buttons. Here's how I have positioned the UI elements in the IB.
This is how I want them to look like in the app as well. The label set on the left side with the same width so the elements on the right side will adjust their widths accordingly.
I have set the leading space to superview constraints on the labels on the left side and set vertical spacing between each of them. Similarly I've added trailing space to superview constraint to the labels and buttons on the right side with vertical spacing between each other. I've also added horizontal spacing between each couple of elements (ex: Name <-> John Doe, Telephone <-> button).
When I run it, it looks like this.
The elements only take up widths as their content needs. I assume this problem occurs because there is no way to auto layout to decide on a width. If I set the labels on the left to same width constraint, it doesn't work at runtime because the constants for them are fixed so they looks like this.
How can I make them appear properly like I have them in the interface builder?
Thank you.
I uploaded a test Xcode project with this issue here.
In your final example, it looks like you almost achieved what you wanted. You made all the labels on the left have the same width. You can now set a width constraint on the first label to a width that works for you, and the other labels should automatically match. The only issue you might still have is getting it to look good on wider screens.

Set fixed space between 2 views in Interface Builder

I am trying to learn to use layout constraints in interface builder but I am running across an issue...
I've got two views, one UILabel and one UIImageView, and I'd like a layout as follows:
the label should be centered in the superview
the imageview should be a set distance to the left of the label
Seems pretty simple, but I can't figure out how to impose a constraint for the second condition (the first is done automatically). Anyone know how to do this?
Thanks!
Click on both views while holding the command key so they're both highlighted. Then click on the pin button in the bottom right hand corner of IB (the middle button). Select horizontal spacing and the constraint should now be added. You should be able to adjust it as needed from there.

Resources