iOS Auto Layout - Impossible case? - ios

I need to create a launch screen Storyboard with 3 views. The middle view must have a 4:3 ratio, the top view must occupying 30% of the remaining space and the lower view must occupy the rest of the space.
For example, when the view is 375 x 667...
Middle view: 375 x 4/3 = 500
Top view (667 - 500) * 30% = 50
Lower view: 667 - 500 - 50 = 117
I tried everything I can think of (including using a UIStackView) but can't find a way to make it work.
Is it possible? What would be the right way?
Many thanks!

You can't easily do that with autolayout, as you rightly say, because the thing your first view should occupy 30% of is not a contiguous piece of space. You can do it by adding two invisible views, to measure the "remaining space" with the height of a single view that you can take 30% of, but it's not worth the trouble. Just implement your requirements in code directly in layoutSubviews or similar.

It's possible, You can archive this by using equal height constraint with proper multiplier. Following are the steps in your case:
Add all 3 views with required size as per screen size.
Set equal view hight constraint for first view to super view as bellow.
(Multiplier is set by considering you are working on view with size 375 x 667. Change it as required.)
Set top, left & right constraint as required to zero.
Set equal view hight constraint for second view to super view as bellow.
Add other missing constraints to second and bottom view.
You are done.

To get the height of the remaining space, I added a hidden view at the top with a 4:3 ratio contraint and another hidden view below that occupies the rest of the space. I coud then use height constrains relative to that second hidden view to layout my views.

Related

How do I make two constraints match each other in iOS Storyboard?

I have a view that is sized to the width of the screen with a 1x1 ratio of width to height (matching height). I have a view below that square view that is a set height from the bottom. What I want is for the top view to be centered in the blank space. So I would love to just set 1 constraint for the top and 1 constraint for the bottom to say, match. Then if there are x number of points of available, each constraint should be size x / 2.
So far, I haven't been able to find a constraint setting that can do this. Could someone describe how to do this?
Generally you add 2 transparent views, 1 above and 1 below, pin the edges to your existing views and set the 2 new views to have equal height.

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.

Dividing screen in to 1/3 and 2/3 views using autolayout

Is there a way to divide the screen by percentage (not equal percentage)
I need to split the screen to two parts 1/3 and 2/3.
I know how to split to equal parts but cant figure out how to split to non equal.
Thanks
Make an "Equal widths" constraints between your view and the super view with multiplier 2:3 for one view and 1:3 for the other. See picture. "Equal height" if you want to split in the other direction.
Steps :
Drag a Two UIView objects on viewcontroller.
Pin left-view to left, top and bottom of superview
And Right-view to right, top and bottom of superview.
Pin the horizontal spacing between two views(left and right). Make sure constant is zero since/if you want no spacing.
Select left-view and superview and set width equally (hold your patience). Now go to size inspector and edit width equally constraint having left view selected. Now set the multiplier to 0.33 (now % width is set).
See the result :
Using storyboard & autolayout it's very easy: I suppose you need to split the screen vertically (but horizontally it's the same trick).
So using a single view controller, drag&drop two views and set them attached to top and bottom. For the the view 1:
view 2:
Now you need to set the constraint equal height for the view 1 among the main view and the same for the view 2. After that you can edit these constraints settings a value (between 0 and 1) as multiplier. Double click on the constraint:
and set the value that you want for proportion (the trick it's maintain the sum of these constraints as 1 or just put in "2:3" here and "1:3" in the other view):
So this is the result:
I hope this is what you need, tell me if you need more details!
You can use the Equal Widths constraint as in the accepted answer, however, if what you're trying to constrain is not two sizes but rather one point to another, such as constraining one view's horizontal center to the 1/3 or 2/3's point on the screen, then it won't work because you can't constrain a point in space to a size.
But if you know say, view1, in your xib is going to be the full width of the screen, then, for example with the horizontal center point, you can constrain it to that view's trailing edge, and if you want the view to be centered at 1/3 or 2/3's the screen's width then set the multiplier to 1:3 or 2:3, respectively.

How to manage the gap between two views in auto layout

I am using auto layout for my ViewController I know how to define the gap between two views using constraints programmatically. What I want to do now is when the screen size increase, increase the gap also. Because my UIViewsare positioned properly in iphone 4s and 5s but in 6 and 6 plus they are positioned in a small area of the screen. I know adding multiplier we can set the aspect ratio of a view but, how to increase the gap between 2 views when the screen hight increase.
UPDATE
Let say this image,, there is a logo above this please login label.
This is my verticle position constraint
V:|-70-[Title]-130-[lblFirst]-0-[lblSecond]-20-[textusername]-10-[txtpassword]-10-[btnLogin]
this Title is an image 130pix above to this Please Login label. I want to increase this 130 when it comes to iphone 6 and 6 plus
Take a UIView with alpha 0 and clear color and use it instead of the gap and use aspect ratio to the view used for gap and top and bottom or left and right whatever your situation to the views between whom you want to make the gap dynamic. And also you need to add some more alignment constraints to the gapView but it will work. I could not found any other method to do this easily so i have used this method everywhere in all projects and all working fine. I think UIView with clear color and alpha 0 will not effect the screen or performance in any way. We can use this.
Here is an example like if you have two text fields vertically and you want to increase the gap between them dynamically then take a view between then with the exact same width of text field and add those constraints.
1) Top of GapView to upper text field.
2) Bottom of GapView to below text field.
3) Equal width to any text field or leading and trailing to superview or main view.
4) Proportional Height to main view(you can add this constraint by adding equal height from subview to any of superview and then by changing the multiplier);
And your GapView will increase and decrease accordingly.

Center multiple views with varying height using autolayout

This answer: https://stackoverflow.com/a/25898949/1364053 is suitable for my problem to some extent. What I need is there are three views in my container view.
The middle one is a scroll view. If I pin height and width of all three views and use the ratio method as described in the above link, all works fine.
But I need the middle scrollview to grow to some extent, let's say the current height of the scroll view is 30, If the device height is increased it should grow to a max of 40 but not more than that (<= constraint).
Ok..So I tried giving two constraints to the middle view. The <= constraint has higher priorty (750) than the = constraint (250).. but when the screen size is increased.. the height stays the same.
Sample.xib
The uploaded xib might help you.

Resources