Trouble with layout on various screen sizes - ios

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.

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!

Auto layout for uicollectionview

I have a UILabel in my storyboard which i want to align from bottom and whose width and height want to get adjusted in portrait and landscape.
For this, i dragged in a UILabel pinned its leading space, trailing space, bottom space to its superview.
I did not pin its width and height since i wanted its width and height to be changed during its landscape rotation.
With this i got a warning in story board where i have selected to update frame, which automatically adjusted the frame and made it work correctly.
Now i am trying to achieve same kind of behaviour with UICollectionView but this is not working as expected, instead it is giving out some errors saying 'misplaced view' and 'missing constraints'.
I am aligning buttons in 2 rows and 3 columns in the collection view in portrait mode and want to align all the 6 in single row in landscape mode, so if i pin the height here.
The problem is it will go good for portrait mode but in landscape the height will seem higher. Any help on how this could be achieved.?
You got errors because you did not added the constraint for the collection view top layout or height or aspect ratio. it is necessary to assign all side constraint so the view will automatically layout according to the constraint. if you miss one it will give you error or warning. Look you want dynamic height for collection view. If you want to achieve this for all the devices like for all iphones then you need to pin the height and make a outlet of the height constraint and change it in your viewDidLayoutSubviews method. select the collection view then go to size inspector and double click the height constraint. this will show you the constraint on the document outline. make outlet of that and change that constraint according to your need like on portrait or landscape you can calculate with logic. you might need to change the constraint according to the orientation change. follow https://stackoverflow.com/a/24072010/4030971
i know that will be great if all those stuff work only with some constraint but i found this way easier for me.

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.

How to shrink height of UILabels if needed via Auto Layout

I have several UILabels laid out on my View Controller. While working in Portrait mode, I have increased the height of each label so that the boundaries are all touching - they're stacked on top of each other without any spacing in-between. I have a big title and button located at the top and bottom. I have already fixed the top title to always be centered at the top, and the bottom button fixed to the bottom. I also have fixed the middle label to be vertically and horizontally centered in the view. That's all working great. My problem is, I can't seem to figure out how to get the labels to all fit on screen and mostly vertical centered collectively when the height is reduced. I would like it to reduce the height of each label as needed (but not too short such that the text is clipped). I've tried pinning the heights and then changing them to less than or equal to, but in doing so it wants to update the frame to remove the extra height, causing the buttons to no longer be stacked right on top of each other. Then if I set vertical spacing constraints, it will be fixed so it will result in the labels too close together in Portrait if I use the standard value, or if I go with a fixed number they won't adjust to fit in Landscape.
What do I need to do in order to have the labels expanded to fill most of the view when in Portrait, shrink a bit on a 3.5" screen, then really shrink when in Landscape? The labels should all be centered collectively, based off that middle label that's always centered. This is basically what I'd like to obtain:
What you want to do is set the label's heights to be equal to one another. Then provide constraints for distances between each label, and between L/R sides and top/bottom sides. This way, the labels will shrink when the screen height shrinks.

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