Auto Layout Challenge: Please tell me what is wrong in this case - ios

I've been trying to understand Auto Layout, but keep failing and need help.
In this test project below I wanted to align 4 squares to look good in both portrait and landscape modes on all devices. I need to know what constraints I am doing wrong.
I used views in this case.
In the first step I added equal width/size pins since all the squares were the same size.
Size of the squares were W:250 H:121. The view container was the inferred option
In the second step I added width and height pins (cushions) between the squares.
In the third step I added all the necessary outside constraints from the individual squares to the superview (leading,trailing,top to top layout, bottom to bottom layout).
And finally what my constraints end up doing; they look fine in portrait but look weird/don't show in landscape.
What am I doing wrong? Is it a a size class issue, a constraint issue, am I completely missing certain steps, or something else?

In your 3.5 and 4 inch it doesnt show because you fixed the constraint to the bottom of your superView, that constraint's constant value alone is higher than the screen itself, forcing your views's height become 0 (or very small on bigger 5.5 screen like in your screenshot), for fixing this, try use less the CONSTANT value, instead, make the constant become 0, and change the Multiplier value on those you want to make it scale with the super view (in your case is the constraint to bottom)
Another option is change the priority of the bottom constraint to lower, and give one of the view a height constraint (Relation set to either equal or greater than or equal) and it will work

Related

Constraints to resize buttons to fit any screen - Xcode swift

I don't really understand constraints and have tried many different suggestions found online. All they seem to do is bunch everything up on top of one another or do nothing at all.
I have the following IPad application but I want it to work on any size device, mainly a IPod touch.
The page is simply two buttons that I want to remain the same no matter what screen they're on.
Any help on this appreciated.
It helps to think about points of reference that won't change with different screen sizes. Sometimes you want things on, say the top left corner so you just do constraints to the top and the left.
I'll give you two suggestions
Suggestion One
For your case, it seems like you might want to do constraints off centerY since you want them to be in the middle despite the screen size.
So I would make a constraint to "Center Vertically in Container" and then tap on the constraint and adjust it's value to negative or positive, so that way it's always X pixels above or below the centerY.
Now that's not going to be enough. it knows it's Y position but it doesn't know its height, width, or X position. So you need to add enough constraints to satisfy those.
A few examples:
X/Width: Two constraints to leading and trailing on each button OR Center horizontally and fixed width constraint. (again be careful with fixed width constraints since screen sizes can change, sometimes it's what you want though)
Height: Yeah just give it a height constraint in this case.
Note that this means no matter the screen size they'll always have the same gap between them (and maybe different gaps to the other edges).
Suggestion Two
Use a container view, either a stack view (fill, equal spacing, vertical alignment, a spacing value for gap between) or normal view.
You can make the view a fixed height based off the height and spacing between the buttons you want. Then simply center that container view horizontally and vertically on the super view.
Nonsuggestion
There are certainly other ways (like using buffer views with equal heights constraints. So you'd have an invisible view on top, a view in between and a view on bottom. and you'd give those equal heights constraints and align the buttons to the edges of the invisible views surrounding them. As long as you gave the buttons a fixed height this would work for vertical constraints) but I think these two would probably be the best.

ViewController with proportional view layout

I am wondering if i can achieve this with Xcode's IB auto layout constraints, as currently i am beating my head against a wall, referring to the image below:
I have three views, and want the upper and lower to be proportional to the screen, and have the middle lock x pixels from each. I am sure i can manually do it, but am also sure that somehow the aspect ratio constraint can allow this to happen.
I am pinning the top/bottom view to sides (0), and respective top/botton (o), laying out the view i want in IB's iPad rendering so the proportions look proper, then setting aspect. Logically this makes sense to me, but it isn't working, so i suspect I am making assumptions about the aspect constraint.
Hopefully this is explained well enough to elicit a saving thought or two. thx.
I'm not sure if I understood what you mean correctly, but is that effect below you would like to achieve?
Take a look at the constraints.
Use equal height or width constraints to the superview and set multiplier to the percentage you want it to take. 50% would be .5. To create these in the document outline drag from the child view to the parent view. You will see the option of equal heights or widths. Choose edit on the constraint and change the multiplier from 1 to the desired number. Cheers. Or see answer How to scale height of views and postion of the views relative to the screen size using AutoLayout

Regular / Regular position sizing constraints for all iPads, including iPad pro?

I've used multipliers in my constraints, to size my views. However in my constraints for position I've used values, which I change for different sizing classes.
However using regular / regular sizing classes for iPad Pro it's positions values aren't big enough, but for other iPads it's fine.
I seriously don't have to add positioning views with multipliers to provide percentage spaces for all my views as well do I?
Right, you don't have to use multipliers because Any value other than
1 creates a proportional constraint. For a Width attribute, for
example, the width of the first item can be set to be twice the
width of the second item
First thing I like to say that the resolution of ipad and ipad pro have abundant difference so in case of yours to add positioning views I just suggest you to use size class and use of priority, constant and relation (less than or equal, equal, greater than or equal) if necessary.
Hope, It'll helpful for you...
What we're after is to set the margin between the top of the screen and the top of the subview with relation to the size of the screen. This means the constraint must be related to the superview's height.
Trying to set the distance between the two views (In the traditional sense) is actually a bad idea in this case. Instead, try this.
Add a constraint to center your view vertically in the superview. Now, open up the constraint in the size inspector (The right toolbar).
If you set the multiplier to 0.01 (0 just puts it back in the center of the screen), your view will go past the top of the screen. Change the first item from Center Y to Top. Now your view will be at the top!
From here, you can change the constant to move your view down a fixed amount and change the multiplier to move your view down further proportionally to the screen's height. The formula would look like
subViewYPosition = constant + (superviewHeight * multiplier)
You will need to adjust the constant and multiplier to suit your needs, but I believe this is a good solution to the problem.
An example of this is shown below.

Xcode Auto Constraints not Working Properly

For some reason when I add constraints, by clicking "Add Missing Constraints," it actually looks worse than without constraints in the iPhone 4S simulator.
Here is how it looks in Xcode:
Here is how it looks in the simulator:
Notice that even with the auto constraints, the top text is hidden in the simulator. I want it to look exactly like it looks in Xcode, but I cannot figure it out. I have played around with the constraints for hours now to no avail. It looks better and better with the bigger screen size I use in the simulator, but I need it to have a universal look across all devices. What am I doing wrong?
First remove all constraint for the view.
1) Apply pin Width and Height constraint for your all three controls.
2) Apply Top space from Top Layout Guid for all three controls.
3) Apply Align Centre Horizontally in Superview of your all your three controls.
After applying above constraints your controls will appear in centre in any device.
I believe the three controls should be centre aligned with the top view and the vertical space between the three controls is constant.
The controls will automatically adjust their width according to the text given. So all you have to do is to only add the constraints for their positions. So each control will need two constraints, only for Y position, another for X position.
And of course, you can change the multiplier of the constrains to support the Landscape orientation.

Issue with iOS storyboard view constraints

i am having an issue with storyboard constraints, i have a map view, which i would like to have a variable height, depending on the screen size, e.g. (iPhone 4 & iPhone 5). The buttom section should contract and the mapkit height should contract with it. I am having a hard time understanding the constraints system, i have tried setting distance top & distance bottom to 0 & 0, i would expect that this gave the view a variable height, but it does not seem to be the case.
And this is how it looks on the iPhone 5, which is how its supposed to look.
EDIT 1
I have read your question 3 times and am unsure of what you try to achieve.
So, the result is okay for iPhone5 but not for iPhone4. What do you want exactly to achieve for iPhone4? having the same height for both sections 1 and 2, and only having the height of section 3 decrease?
If so, it is quite easy:
- Put a height constraint for section 1 (height=X)
- Put a height constraint for section 2 (height=Y)
- Put a distance constraint between 3.Top and 2.Bottom (=0)
- Put a distance constraint between 3.Bottom and Superview.bottom (or Layout) (=0)
Try selecting your view controller (not the view!) in Interface Builder, at the utilities part select attributes inspector, and try toggling the checkboxes at the Extend Edges part. Additionaly, at your screenshot I see a red arrow symbol indicating that the system cannot satisfy the layout constraints, before all, make sure you satisfy all the constraints.

Resources