Xcode 8 constraint help required - ios

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!

Related

Trouble with layout on various screen sizes

My screen layout has elements placed correctly
but when I switch to another screen, eg. iphone 7 plus
or se
things change. I've set constrains, height and witdh when I suppose I should, but apparently it does not want to cooperate with me. Please tell me what constrains and heights, widths should I set.
Add below constraint
For the red and green View You need to give Equal Width Constraint.
For Blue View at the center you need to give Center Horizontally Constraint.
For the bar at the bottom You need to give Bottom distance from the Red/Green Top Constraint.

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 autolayout changes leading and trailing space

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.

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.

iOS Auto Layout >> View is not Changing its Size

I have a design for a screen that should look like this (other things will be added later, but I cannot seem to resolve the basis...):
I have added Constraints to determine the following:
Both Labels are Constraint in spacing to the screen edges.
Middle View is Horizontally and Vertically Constraint to the Middle of the Background View Center.
I have added 4 Constraints to express Minimum and Maximum Vertical Spacing between the Middle View and the Labels (Current spacing as Maximum and Standard spacing as Minimum).
I have also added 2 Constraints to the Middle View to define Spacings from the Screen right and left edges.
I thought that it should be enough, but in reality, when switching between Retina 3.5 and 4 the Bottom Label disappears and the Middle View is cut in the middle:
I have tried lowering the Middle View Content Hugging and Content Compression Priorities, and still no good.
Here are the Warnings I get:
Any idea how to resolve this?
Or alternatively, how to approach it differently (preferably, still using Auto Layout)?
Add Equal Width & Equal Height constraints as well & It will work
Add TopSpaceToContainer constraint for Top Label. Then add width and height constraints for your yellow view at the middle. Remove the multiple vertical spacing constraints given to the Top Label and Bottom Label.

Resources