Aligning two button images at a fixed distance in ios app-Swift - ios

I am working on an ios app where I want to have two buttons at a fixed distance from each other. Something like below:
I tried dragging one constraint between the two images. But when I run it in simulator it shows overlap as below.
Can someone guide how to put both the pictures at a fixed distance. Also, I am seeing when I turn the simulator as right in hardware option some of my buttons are not getting auto aligned to horizontal screen and doesn't get displayed. How to fix this too?
Thanks

First of all clear all constraints. and follow below steps :
select both button with command button press
Give Width & Height constraints for both. (Pin at bottom-right side)
give Horizontal Centre in container constraints for both. (Align at bottom-right side)
Now, update all constraints (Resolve Auto-Layout Issue bottom-right side)
Hope, it's work..

Design your view according to this image
Drag a new view and then put your two button in it
Constraint for new view
Top
Horizontal center
Fix height and width
Constraint for left button
Top and leading
Fix height and width
Constraint for right button
Top and traling
Fix height and width
It will work defiantly.

Related

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.

How to center TextField using Xcode constraints?

I am new to iOS and am trying to align these TextFields in iOS. Below is how I would like it to look on an iPhone 7.
However, on the iPhone 7 Plus, the TextField shifts to the left of the center, despite my constraints.
Why is this? I have a constraint that aligns them to the Center, as seen on the right part of the screenshot.
It is very simple!! Just follow below steps,
Open you .xib or .storyboard file
Select view you have to add constraint
Click on align button at the right - bottom corner
Set horizontally in container constraint 0
Then press add constraints button
You can add any constraints to any view by following these steps
You just have to seth trailing and leading space fixed means both left
and right indicators should be red as shown in the screenshot.
For more details see the screenshots:
It will remain fixed from left and right in all the screens.
in your case is important set the width constraint.
However you can use trailing and leading constraints, in this way you will not need to set a defined width for your textfield and the position is correct for all type of devices.
If you want design like this so you have to take UITableview then take UITextfield for every cell beacuse you use every cell as view so easily manage all textfield and constraint. And also easily scrolling if device is display small.

How to make storyboards for each device

Actually, I made the view showed below. But every time I run the app on different devices they're moving their places. I added constraints by using Add Missing Constraints. You can see the problem I have. Can I solve it by drawing all storyboards for each device and how?
Screenshots
Here's how to get the view the way you want it using Storyboard. I'm assuming you know how to add constraints in Storyboard.
For the yellow circle, set height and width constraints Next add a center horizontally constraint. Finally pin it to the top.
For the green and blue circles set height and width constraints. Now set the height and width of the green circle equal to those of the blue circle. Pin the green circle's edge to left edge and the blue one to the left. Finally pin both to bottom.
Alternatively for the latter circles, you could choose to add both to a third view with the same constraints in said view and center the extra view horizontally while making sure to set its height and width constraints.

Xcode IB auto sizing?

Everything looks fine in IB. When I run my app using an iPhone5/iPhone6/iPad sims, it's completely mangled.
In IB:
In iPhone6 sim:
In the above sim, the buttons are cut. The textfield and textview are also cut. For the TF & TV, they are aligned center but trail off the edges on both sides.
I have the buttons width set
Editor > Pin > Widths Equally
The title label is set with a
Horizontal Center in Container
constraint
The textfield and textview also have
Horizontal Center in Container
and
Editor > Pin > Width
Is there some way to fix this?
-- EDIT --
After a few tries with constraints, looks like I have everything working except the two buttons.
Current listing of constraints:
As suggested by others, you should consider using the Auto-layout feature if you plan on constructing your views using the IB.
Here are some tutorial links:
RayWenderlich.com Part One
Youtube video This one covers a bit of size clases
Hope they help.
UPDATED:
I've read your updated post, you need to add width and height constraints. The view you see in the IB right now is 600 by 600 points, and the simulator one is smaller, which means that if you leave it as is, when you run the app you'll only see what the iphone screen has the capacity to show.
You need to add more constraints than what you used, try defining an equal width for the buttons, and assign the left one a left margin constraint, the right one a right margin constraint, give both of them vertical spacing constraint related to the text view or long label, the text view or long label should have left and right margin constraints, a height constraint and a top constraint to the textfield, the textfield should have a vertical spacing constraint to the label, also left and right and height constraint, and last the label should have leff, right and height constraints plus a top constraint to the main view.
I feel that those are all you need, but Xcode will through warnings at you if it feels you are missing something.
Try it, and let us know.

iOS 6 Autolayout not resizing properly

So all I'm trying to do is put an imageview at the foot of the screen. Before iOS6 autolayout I could just hit the bottom "I" and it would hold it to the bottom and push it up if the screen got short (i.e. if the screen size went from 568 to 480 points tall). But now I have been trying to add a bottom spacing to Superview at 0 but I can't seem to do anything to get rid of the the top spacing constraint to stop messing me up. If I convert the top spacing to a user constraint and then delete it then (thinking that the vertical constraint would be handled by the bottom spacing) but it auto re-creates that contraint... any ideas?
Update It seems it was due to the fact that I was having it be a subview of a scrollview (was going to use it for scrolling horizontally, not vertically). Anyone know why it wont let me do that? Seems like you should still be able to constraint to the bottom of a scroll view.
It could be that the Button's constraints are somehow wrong. Having the height, width, bottom distance and horizontal centering should be enough to define its position. Try deleting the button and centering the image view on the super view instead, and see if tha allows you to delete that top constraint.

Resources