iOS autolayout changes leading and trailing space - ios

I have some issues with auto layout, hopefully some of you can help me =).
I add a UIView to an empty view controller, like this:
Then I add constraints for leading and trailing space, like this:
Which results in this:
I can't understand why it sets them as equal to -16.0. If I change those values to 0, then Xcode complains about the width of the gray UIView.
When I run the app on iPhone 5 simulator, it looks fine:
However, when I run the app on iPhone 6 Plus simulator it adds white space on the left and on the right:
I don't want the gray UIView to have any space from the left of from the right no matter how big the screen is. How can I fix it? Thanks!

Double click the constraint and open it. There select first or second item and deselect "Relative to margin". Then set your constant as 0.
Xcode is showing -16 because it is calculating space with respect to a margin and not the edges.
Check following link for more info on margins. Editing Auto Layout Constraints

You can clear all existing constraints on the view,and in Any*Any mode, add constraints for all 4 space to superview, which are Leading, Trailing, Top, Bottom. Then it will have the equal width with the screen no matter how big the screen is.

Related

Xcode 8 constraint help required

I am new to IOS development and have the attached screen that I am developing in portrait mode only. I have fiddled around with constraints for half of today but not really got anywhere with making this work on anything other than a 5s.
Not sure if i constrain to edge of phone and/or the box at the side of the one i am trying to constrain or both!
It would be much appreciated if someone could point me in the right direction to get this to fit on all screen sizes. Landscape I think is a pipe dream at this stage:)
Here are the width constraints you should add to each button:
add a equal widths constraint for each button to the main view, then set it's multiplier to 0.5. That will make each button half the width of the screen.
for the dark blue buttons, make a leading constraint to the left edge of the view (not the margin) and set it's constant to 0.
for the light blue buttons, make a trailing constraint to the right edge of the view (not the margin) and set it's constant to 0.
For the Y positioning, you should do the following:
for the top buttons, add a vertical spacing constraint from the buttons to the title view, with the constant set to whatever you want
for each button that follows underneath, add a vertical spacing constraint to the button above it with a constant of 0
for the bottom buttons, and a vertical spacing constraint to the bottom layout guide with a constant of 0
This will adjust the height of the buttons to match the screen size / orientation while maintaining your layout. should work fine with landscape, although those buttons will be awfully skinny!

How to scale up apps in XCode for diffrent screen sizes

I'm working on an app for iPad but when I set the contrains(so it works on iPad Pro/iPad Mini) everything moved. I don't know if I set anything wrong or if it's problems with XCode However, are there a way to just like scale up/down the screen without contrains? Or how to fix the constrains problem.
(I choose all buttons and click add missing constrains)
Picture:
IMO selecting add missing constraints is not exactly what you want. If you want your view to scale up when screen is larger, you need to set the constraints to its superview. So you just say I want it to be 10 pt from the leading edge and 15 pt from the trailing edge. By adding missing constraints Xcode probably tells the view to be xy points wide.
To set the constraints as I mentioned you need to Ctrl drag from the view to its superview and add leading constraint. Similarly for the trailing and eventually top and bottom constraints.

Constraints with Label on Image View

In my app I have a Image for my background with labels on top of it that are not moving correctly on the iPhone 6 Plus. I am having trouble with the constraints to make the labels move correctly with the image. With the screenshot I attached what constraints should each label have to keep it aligned with the image? Thank you!
screenshot
this shows the suggested constraints
constraints
Your constraints aim on left alignment. So that's way it only works on the size that you were building on, and other display sizes are not aligned properly.
Try to, at least these:
add left leading constraint between superview and textField
add right trailing constraint between button and superview
add spacing constraint between textfield and button
set exact width constraint of your button.
... then add suggested constraint or add your own to them.
Like this (iPhone 6):
View as iPhone 6 Plus:
View as iPad Portrait:
... etc
Is that exactly what you asked?

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 view height and auto layout

I have a viewController, which is configured for the iPhone5 screen size. In red rectangle is the view.
To make this view show properly in iPhone4 - I used auto layout. But when I specify constrains such as: leading edge, trailing edge, bottom space and top space (in top space I even made constraint's priority to LOW ) - the view still goes partially down the screen as if my constraints don't work.
BUT if instead of top space constraint I specify view's heigh and delete top space constraint - everything works perfectly.
Does anyone can explain it please? Thank you.
Thats because when you set up the top space constraint it will move the view by the constant you provided. iPhone 4 and iPhone 5 screen height is different but the constant remains the same so obviously it will behave differently. One way to troubleshoot your interfaces is to switching between iPhone4 and iPhone 5 on storyboard device on storyboard (first button from left to right on the bottom right corner of interface builder).
Auto-layout is all about experience in my opinion. I struggled alot with it until i learned. If you want your view to be attached to the bottom of the screen you should set the BOTTOM SPACE to 0 and specify the view's height like you did or adjust constraints for it's subviews so that the height is set dinamically according to the views inside.

Resources