I am new to Autolayout, so I am struggling to understand how to position two UITextFields side by side. E.g.:
I have tried a number of constraints, but I cannot figure out how to correctly position them. When running the app.
Any insight on how to do this?
Try pinning the top, left and bottom of the check in textfield to the superview. Pin the top, right and bottom of the check out textfield to superview. And the left constraint of check out to check in using the 'pin' button (bottom left of the storyboard), a box between two lines icon.
Other option is to create a Stack View with these TextFields
You have the views like this now:
Now, you have to select the Constraints to the Stack View:
And then you only have to select Fill Proportionally in the Distribution:
Final result:
Related
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.
I'm hoping someone can help me out here. I set where I want the items to be using constraints and when I run and build the app it seems to push everything to the top. This occurs even when I don't put any constraints on any items. I've attached pictures if they help at all.
Have you tried setting the Top Constraint of your topmost subview to "Top Layout Guide" as seen in this screenshot? (Looks like your Top constraint is to the 'Superview' instead!)
http://i.stack.imgur.com/fpHAQ.png
Making sure your view is selected go click on the triangle constraint button in the bottom right and select clear constraints under all views.
Place the views where you want them. Then for each element click the square constraint button on the bottom right and do this:
Uncheck constrain to margins
Pin to top bottom and both sides
Check width and height
Then select the triangle and choose update frames for selected view.
Fine tune position by by selecting the view you want to move and updating the constraint value.
Because sometimes its just easier to start again than to try and fix constraints.
I've figured out a solution. Somal was on the right track in terms of the Top Layout Guide. I simply had to right click each individual object to drag onto the view and select Vertical Spacing to Top Layout Guide (See picture) or I could of simply delete the entire ViewController and start from scratch with no copying and pasting from the old View.
VerticalSpacingMenu-Image
Why the pin constraint doesnt work? I have no constraints, and I want to pin one view to bottom of main view, so I select the child view, then I click the icon from "w:Any h:Any row" in Interface Builder bottom right (Pin), then i click the bottom "I" to create bottom constraint, but if I resize the controller view (in design mode) the view is not moved but something like this appears: (The red line with number - instead of moving the blue view to bottom).
I have pinned it to "bottom layout guide". I also tried to pin it to "view" from dropdown while pinning, but that doesnt work too.
If I look on the "Issue navigator" there is only warning saying "Position and size are ambigious for View"
Without any other other constraints there is no way for Xcode to know what you want to do with the view, all it knows is that you want the bottom of your subview to stay 99pts away from the bottom of the superview, but doesn't know what to do with the top, left, or right edges.
Try adding constraints for the remaining 3 edges, it can be as simple as setting the width and height (the options under the bottom edge in your screenshot), or something more complex depending on what you want to achieve.
You need to define the position of view properly. So that constraints can define position and size of the object.
Like in your case you have only provided constraint from the bottom, which will not be sufficient to define position. So you need to define either Height and Width constraints or top, left and right constraints.
And as long as long red line is being shown keep adding constraints.
I was trying to put two buttons right next to each other, one taking up the left half of the screen, and the other taking up the right half of the screen. I found it perplexing that when I set Horizontal Center In Container for both of them that the left button had the property set correctly (center of button), but that the right button had the property set to the center of the View Controller!
I looked into this further and discovered that it takes this behavior whenever its on the right side:
In my pictures, I'm using a UIView with a black background, but it is applicable to anything. Is this the desired behavior? How else would I have two buttons laid out like I stated above?
Why do you use Horizontal Center In Container for your task? It should make both buttons appear in the centre of the container.
Do this instead:
Choose both buttons and set "Equal width" constraint.
For the left button, set leading constraint to the superview, and trailing to the right button.
For the right button, set leading constraint to your left button, and trailing to superview.
You can put your two buttons embebed in a UIView then put a Horizontal center Constraint in that UIView
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.