How to constraint from the center in Xcode - ios

I'm new to Auto Layouts and the concept of constraints. I'm currently trying to create a single view app with two buttons:
The buttons must meet the criteria shown in the image above. I can do everything except the two constraints where the buttons have to be a 30 above and 30 below, as it resizes the entire button to the point that one overlaps the other.
Is there any way to achieve this? I'm not doing the constraints programmatically, but rather on the storyboard. Many thanks.

Try put both of them in a view, add constraint between them 60px height, then make the view center of the superview, leading and trailing 20px, optional 50 from top and 150 from bottom, you should get the desired result.

The main issue here is to ensure that after setting alignment constraints both from the center, to select the following option of either the "bottom" or the "top" depending on how you want it:
This ensures that there is no "overlapping" caused.

Related

UISTackview constraint error in a UIScrollview in viewcontroller

I was trying to add a UISTackview in a scrollview. I believe the UIScrollview setup is right as the following screen has scrolled. Please note that it does not have any stackview, I have added a button with big top constraint so that I can see whether the scrollview works or not.
Next, I added the stackview and just added the top, left, right constraint but not bottom constraint like the following image. However, since there is no bottom constraint, the scrollview does not work.
When I added the bottom constraint, it shows the error like the following image because none of the textfields has height constraints.
If I change the distribution to "Fill Equally". The textfields are taking too much blank spaces depending on the screen size.
How can I use the stackview inside the scrollview. I want to use it as I want some of the views hidden based on conditions and claim the space. Any suggestion would be helpful. Thanks
Your second image is correct - but you need one more constraint.
Add a constraint from the Bottom of the Stack View to the Bottom of View (its superview), but set it to >= 0
A couple of tips...
rename your UI elements... it gets very confusing to refer to elements by default generic names (you have more than one View, for example).
give your objects contrasting background colors during development... makes it much, much easier to see the frames.

AutoLayout Constraints Applied but not working

I'm new to iOS programming and seems to be having a hard time working with constraints. I added a textfield to the view and tried to position it where I placed it on the view. I added few constraints using the "Add New Constraints" button on the bottom right of the storyboard but the textfield position is still off when I run it on a simulator. Can someone help me please?
Update:
I tried adding these constraints but still no luck
So, for some bizarre reason, it started working after I changed my "view as: " option from an iPad to iPhone. Its not a proper solution but I am just going to use it temporarily.
You need right click the button in your storyboard and drag the constraints line to the related component.Then edit it.
demo
You can also add constraints by code,like vfl or masonry.
Every view in auto layout need to calculate position(x,y) and size(height, width) so that we have to add constraints to that particular view
For example:
mytextField I would like to align it in top of view with 10px from top of view and left, right 10px, height is 100px
Give constraints like place textfiled in view and add Top constraint and set value 10, add leading constraint set 10, add thrilling constraint
set it 10, and finally, add Height constraint 100
Then the text field will align at top of view with the required frame on any device.
you are using 'Horizontal center and vertical center' constraint, when you run app on different device other then the device you are using for design, TexField will be on different place because vertical center and horizontal center will be different on that screen.
User top, bottom,trailing and leading constraint to achieve this.

Issues with AutoLayout

The Bar at the top is a view in a view that contains 10 views, each the same size however I am unable to get them to look like this on the simulator. What constraints settings should I be using?
Also is there a better way to have visually represent all of these boxes without using views? it can't be an image as the individual views will visible depending on remaining battery.
What I am using: I am not currently using any constraints as I was only able to make it worse.
What it is doing now:
portrait:
Landscape:
You just need to give leading, trailing and equal-widths constraints between adjacent buttons.
Check my answer here, I've explained with screenshots and steps to achieve this:
What is the best approach for horizontally aligning 5 buttons with Autolayout
Let me know if you want more clarity.
Select all the bars at once and then apply the following constraints:
Check the equal width. which will make all bar of same width. 5 is the margin from top, bottom, center, leading and trailing.
Press Add x Constraints.
At the end update frame by pressing option + command + equal simultaneously.
The first has left, up and bottom constraints and then it is linked to next one on the right and the next is linked to the next and so on with the horizontal leading option.

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.

How to center horizontally two labels?

I have pretty straightforward requirements where two labels must be centered horizontally. So, I have selected them and chose Editor->align->center horizontally. Then added top space to container constraint to both of them. I also need the labels to shrink/grow regarding content size. However, IB shows errors and several warnings. I could make the labels shrink/grow just by adding pin between them (horizontal space) but they will not be centered in that case. Here are the screenshots:
here are the errors and warnings:
UPDATE theraven gave an interesting suggestion to use dummy view for centering it horizontally and pinning two labels to it. I have removed all existing constraints, added this dummy view and center X + center Y constraints to it. Then pined two labels to it (added horizontal space constraints). However, I still get a bunch of errors and warnings:
UPDATE2 Just updating the question, but still no valid answer found. #Theraven workaround works for iPhone4, iPhone4S, iPhone5 and iPhone5S, however it's not real centering but rather a workaround. Therefore for iPhone6 and iPhone6 Plus it doesn't work as leading and trailing spaces will be fixed and won't automatically resize for larger width.
What you could do is add both labels to another view, like a container view. Then you need to center this one horizontally and add the necessary constraints.
To add the containing UIView, you can select both labels, go to Editor -> Embed In -> View.
Then you would need to add constraints to make the containing view fit the two labels. So something like this:
First Label (left one):
Leading Space to Superview
Top and bottom Space to Superview
Horizontal spacing to the next Label
Second Label:
Trailing space to superview
Top and Bottom to superview (or align top with the first one)
Then the containing view should resize as to fit both labels. Then all you need to do is add the top offset constraint for this container view and a horizontal alignment it in the parent view.
This way, the containing view will grow as much as it needs to fit both labels and the space between them and will always be centered in the parent view.
I took a screenshot of my test constraints in case it helps you more.
Hope this was what you were looking for.
To solve this use a blank UIView in between your two labels and center it horizontally. Then pin the two labels either side of the centered blank view. It is common convention to use spacer views like this in auto-layout.
I really don't like the idea of adding another view just for sake of estethic.
Another alternative is to horizontally-center the left view, and horizzontally space the right view of an amout X with the left one.
Then to give the horizontally-align contraint of the first view a negative value equal to the first view width plus half the views distance. Or use multipliers as said in a previous comment.
But this only works with fixed width views i guess.
Use centered UIStackView as a container for two labels with a spacing required.
I didn't really understand what you wish to do.
The error you get (in the first screen shot) is that you are missing constraint for the x position of the labels.
For UILabel you must have constraint both for y and for x position regarding to the container view, when you selected them both and chose Editor->align->center horizontally, you just say that label1.center.x = label2.center.x.
You still need to say where they will be in the container view, you added the top space to container, so you do have the y position, but you didn't say where the x position should be.
You said
I have pretty straightforward requirements where two labels must be centered horizontally
But where they should be in respect to their container?
thanks
Using spacer views is the best possible solution I could figure out, even though it looks ugly for the developer. The user wouldn't even know what's going on behind the scenes and once you have the spacer UIView, you can always reuse it.

Resources