iOS Auto layout in percentage - ios

I wanna create a simple iOS application using autolayout that displays three UIViews. One on the top, one on the left and
the other on the right. The top view should be 50 pixels high and fills the horizontal space completely. The left column should fill 40% of the horizontal space and should fill 100% of the
vertical space. The right column should be 60% of the horizontal space and 100% of the vertical space. Finally in the left UIView wanna add a single button.

Yes, You can.
Add first view at the top, add four constraint, top 0 left 0 right 0 and height 50.
Add second view at bottom of first view, make it top 0 left 0 heigth 100.
Here is the trick, choose first and secound view, then make them equal width.
Choose secound view's width constraint, and change the multiplier to 0.4.

The two existing answers didn't work for me. My answer was built using Xcode version 10.1 (10B61).
The core question is "How Can I Set a View’s Width to be a Percentage in a Storyboard". Answer: the view's width needs to be associated with the superview.
Explanation:
In the Main.storyboard (already containing a View Controller and a View), add a View. I’m going to call it YellowView). And change its containing view name to SuperView.
In the YellowView's Attributes Inspector change the Background to Yellow.
Click on New Constraints and Top = 0, Leading (left) = 0, height = 50
Control Drag from the YellowView to the Superview and let go, in the resulting dialog select "Equal Widths"
Note: YellowView's width becomes the width of the SuperView.
In the Storyboard, Double-click the width constraint we just added and change the Multiplier to 0.25 (Note: Typing .25 will not work - the leading zero is necessary)

Your constraints should be like,
first view - top,leading(left),trailing(right) and fix height
left view - top, left(leading), bottom and equal width with superview with multiplier 0.4 (or width constraint with 0.4 multiplier)
right view - top,right(trailing),bottom and equal width with superview with multiplier 0.6 (or width constraint with 0.6 multiplier)
hope this will help :)

Related

How to I align 4 buttons together? (autolayout)

I'm trying to align 4 buttons together so they look like this:
I want it so that ONE has a 0 margin to the left, right and bottom, TWO has a 0 margin to the left, right, and bottom, THREE has a margin to the top, left, and right, and FOUR has a 0 margin to the top, left, and right
In order to support iOS 8, I do not want to use stack views
You can add 0 constraint to all their sides, then choose all at same time and give them equal width and height, if custom height then just need to set height constraint to 1 of them, then you can achieve the design
You can set proportion width for all four buttons. Set width = Superview.width and then Set multiplier =0.5 This will make sure that your button width is half of buttons superview so will fit two button in it.
To do so, Right click on Button, Drag to super view and select EqualWidth constraint. then go to constraint and edit multiplier of recently added width constraint to 0.5 (or 1:2).
You can check this video for detail
Use Ctrl+click from one button to other button and choose equal width.All the buttons will have equal width in this way.

Need help to set autolayout

Am new to iOS & am facing it very difficult to set autolayout. Watched many videos to learn, but all of them giving solution to a specific problem. No video covers all base rules to set an UI object into it's place & with proper flow.
I came from Corona Background & used to set UI programatically very well. Am thinking here same way, but I think apple made it so difficult or people are not able to explain me properly.
Please see 2 images attached in this question & tell me rules to apply to achieve this UI. I request you people to please explain in the general manner so that my other screen can be completed using same rules.
Image 1: http://i.stack.imgur.com/MPE47.png
Image 2: http://i.stack.imgur.com/qEiCm.jpg
A really helpful guideline is
Every element should be able to figure out its position (x and y) and size (width and height).
Ensure that every element only has one way to figure out its position and size.
Remember that the autolayout of all the elements can influence each other.
The most used layout constraints are:
Top The space between the top of the view to another view.
Trailing The space between the right edge of the view to another view.
Leading The space between the left edge of the view to another
Bottom The space between the bottom edge of the view and another view
Width Assign a fix width to a view (Note that it can also be a percentage - aspect ratio)
Height Assign a fix width to a view (Can also be a percentage)
Center Horizontally Always align the view relative to the horizontal center of another view
Center Vertically Always align the view relative the the vertical center of another view
For example in your second image, say the yellow bar is a UIView called titleView.
Set the position of titleView by setting the top layout constraint to the container view. y position is set.
Set the leading constraint to the container view. x position is set.
Set the trailing constraint to the container view. The view's width will now stretch with the screen size. Thus width can now be determined.
Set height to 50. Height is set.
Now... If you also set the width of this view, it will cause the layoutConstraints to break, because you have redefined the width constraint. Some of the constraints will then be ignored.
Another example of how layoutConstraints might influence each other. Lets look at determining the y positions of the second image.
Say titleView has a top constraint to the container + height of 50.
currentCampaignView has a top constraint to the bottom of titleView. (Use vertical spacing) + equal height to titleView. (y + height can be calculated)
the 5 buttons have equal heights. Top buttons have Top space to Bottom of currentCampaignView. Centre buttons have Top space to bottom of top buttons. Bottom button have Top space to bottom of centre buttons.
startCampaignView has equal height to currentCampaignView. Top constraint to bottom of bottom button and Bottom constraint to container view.
Note that because views and buttons have equal heights, all are considered when determining the height. Thus it is very important that they are all interlinked and that the entire height that can be used is specified. In this case it is specified by the first element titleView that has a Top Constraint to the Container view (of which the height should be known) and the last element, startCampaignView, that has a Bottom constraint to the Container view. Because all the views in between are linked on y position and height, the view can work out what each view's height and y position should be.
One more example. (Your first image)
topLeftButton Set the top constraint to Superview. (y), Set the leading constraint to Superview (x), Set height = 100 (height), Set equal width to topRightButton (Note that we do not quite have the width yet, because the width of topRightButton can not be determined)
topRightButton Set the top constraint to Superview. (y), leading constraint to topLeftButton (will be used for x), Set trailing constraint to superview. Now the width of both buttons can be determined, because we have an external startX + endX and we know the two buttons touches each other and are equal widths. Thus the available space will be split to get the width of the two buttons.

how to design auto fit for all iPhone devices like 4,5,6 and 6+ using storyboard auto layout

I need help to design the layout for 3 rows and 2 columns of uibuttons in a single view controller on storyboard. Its fit for all iPhone devices like 4s, 5,6,and 6 plus. I don't know how to use the constraints. Please guide me to do this. Here is the sample image i want like this
I have setup a github repo regarding your problem link
Steps:
1) Top Bar height = 0.17 * superview.width
2) Bottom Bar height = Top Bar height
3) MidSection covers all area between top slab and bottom slab
4) Row1 height = MidSection height * 0.33
5) Cell1 width = Row1 width * 0.5
These are the major constraints, hope it helps
Follow these steps.
Fix the height of the Top View(In your case the green view). Give the Leading, Trailing and Top constraints to the Superview as 0.
Fix the height of the Bottom View(gray one). Give the Leading, Trailing and Bottom constraints as 0.
Now place a view in the middle of the top and bottom view. Give Leading, Trailing, Bottom and Top constraints as 0.
Place button1 in the middle view. Give the Leading and Top constraints 0 and width proportional to middle view in such a way that the button should be half of the middle view.
Give the height of button1 proportional to middle view with the multiplier 0.3. (Change the multiplier value if needed)
Make a duplicate button of button1. Give the height and width equal to button1. But the Trailing space to middle view as 0. And Top constraint too.
Do the same thing for the remaining four buttons, but give Leading space or Trailing space as per the requirement.
Here the important thing is the height of the button1. You need to check for each value in all the devices. Or else you can simple use a scrollview and fix the content height of it.
Check out the image if needed.
Hope this helps you.
I made a sample project for this. You may take some idea from it;
http://jmp.sh/Xli1hQ1
This guide should help you.
Autolayout guide

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.

Equal widths / heights with parent (root) view?

I'm trying to add constraint in Storyboard to an UIImageView to it's superview, equal widths or heights but it's disabled.
I tried selecting both the view and the superview, then the add constraints button at the bottom right (mentioned constraints appear disabled), I tried dragging from view to the superview (mentioned constraints aren't listed).
I tried removing all the constrants related to my view. Didn't make a difference.
I'm using a xib, of a custom view. The parent view is also the root view. XCode 6.2 beta. Using size class Any/Any.
Any idea? Thanks.
For making UIView height/Width equals to super View.
Here's what you need to do.
You can set leading,top,tailing and bottom constraint by right clicking to View and select superview.
After setting Top and bottom constraints as i've set in previous image. You need to update constraints to '0' as it might have top and leading space not equal to '0'. You can select those constraints and changes constant to '0'.
You can also set constraints from pin to View as i've set for leading and tailing you can also set top and bottom option in similar way.
EDIT:
If you want to set width and height equally then you need to select both the view and set it's equal height and equal width. As I've set them in below image.
Result:
Below two images shows how you can changes width/ height using multiplier as you need to set constant to 0.9. as from storyboard or xib you cannot set constant in float. So you need to set multiplier.
First Image I've set multiplier in ratio.
Second Image I've set multiplier to 0.9
If you still face any queries please let me know. Hope this will help you solve your problem.
Don't give the inner view (UIImageview) any width or height
Add top margin (with superview) with 0
Add bottom margin (with superview) with 0
Add left margin (with superview) with -16
Add right margin (with superview) with -16
Well, I ended adding a parent view in between. This parent is constained to the top view - left, right, top, bottom: 0, and then I can set my inner view to equals width and hight to the parent (the reason I want to do that is to make proportional size - set constant to e.g. 0.9).
No idea why it's not possible to do this directly with the root view.

Resources