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.
Related
I am trying to do the constraints for these horizontals button. I want the ratio of size of buttons to be the same, and the icons to be of equal widths and heights of each others.
Any idea of how i can do that so these buttons resize properly according to the screen size? Thanks!
Make groups of UIView containing the icon and text. Lets call this container view
Place all the n container views inside your storyboard as you would like them to appear. Now:
To the left most container view add a leading and bottom constraint to the super view.
Now to the second container view add a leading space of 0 (or anything you want). Control + drag your second button to the first button. Hold down shift and select equal width, equal height and align bottom.
Now apply the same constraints as your second container view to all your n - 1 container view. n being the number of container view you want to add. Now to your last (nth) container view add, one extra constraint, which would be a trailing space to the superView. Now all your container view ought to have an equal width that will be determined depending on the width of the screen!
If you want to have a specific height or aspect ratio to all your container view. Just add the height or aspect ratio constraint to your first container view and all your subsequent views will get updated accordingly.
OR
If you wish for the height to be dependent on the screen size and not maintain a specific aspect ratio, then you will have to give the first container view a equal height to the whole view with a specific multiplier like 0.15.
You will also have to add appropriate constraints to the icon and label present inside each UIView
Edit: A much easier thing for you to do would be add the icon as an image to the UIButton and add the text as you would normally to the UIButton. The UIButton will appear quite similar to the screenshot you posted. And then just apply the constraints I mentioned above.
Set the width and height as ratio of the SuperView. Set if for one button and for the remaining buttons make the height and width equal to the first button for which you defined the height and width in terms of superview height and size. Use this SO Post to see how to set height and width as ratio of the superview.
Hope this helps.
It's Simple because your All buttons are in single Direction so you can use StackView.
Just simple first apply the equal hight and equal width to all your buttons
now select all the buttons and add them in the stackview
it will be in the right side bottom. (with the constraint icon)
now simple Apply add missing constraint. it will done the work by own and gives better result. (but take care here apply it from the all views in View Controller Section)
And now Bingo try this every Size will show same.
This will work same in simulator also.
The solution is very simple.
See the image below (5 buttons)
The first (blue) button is pinned to the left and bottom of the superview
Each of the other 4 buttons (red, black, green, pink) are top aligned to the first (blue) button
Each button is using a horizontal spacing to the previous button (with a constant of 0). So red button has 0 horizontal spacing to blue, black has 0 horizontal spacing to red, etc
The Last (pink) button is also pinned to the right of the superview
Finally all 4 other buttons are set to have same width to the first (blue) button
That's it!
As for you icons, all you need is to set them to have same width & height to the first icon you have
I used the following constraints to build one screen for my app. However the icon buttons get messed up in different screen sizes (screenshots below). Is there a way to rectify this?
1- Give equal heights and equal widths to all 4 boxes.
2 - For first one-> leading, top,
- For second one(travel and place)-> horizontal spacing to 1st one,leading and top
- For third one(activity)-> vertical spacing with 1st one ,horizontal to utilities and leading
- For fourth one-> trailing , vertical spacing to 2nd one and horizontal spacing to activity
What I would do is:
Set a proportional size (width and height) to the first button, maybe 40% of the container.
Then for the other 3 buttons, you can set them to the same size as the first.
Add some space between them.
And thats it.
1 - Take two views as two rows
2 - Let for first view (row), add two subviews , give the width of first equal to width of superview and multiplier 0.5 , let the second view align leading to first view trailing and width equal to first view
3 - add the button as subview in each view with top , right , bottom and left spacing as desired. Repeat the same for 2nd button in 2nd view.
4 - Repeat the above steps for row 2 view
hope this helps
These are actually pretty straightforward when you think about how all the views are supposed to relate to each other. I made a demo storyboard to show you how I'd wire it up. The key things:
Each box has a 1:1 aspect ratio.
Boxes in a row have equal widths to each other
Each box is constrained on both sides to the edge of the view or each other
The icon's height is set to a % of the box's height
The labels are pinned to the top, right, and left of the box
Obviously you'll want to adjust your styling as appropriate, but that should get you the effect you want.
So I'm just trying to wrap my head around Auto Layout; I understand how to use constraints, just not how to apply them appropriately. In the picture below I had set no constraints and simply want this simple design to resize correctly on all size classes (Horizontally and Vertically).
http://tinypic.com/r/2ymxbop/9
What constraints would I need to put in to have these squares resize appropriately on all screens (horizontal/vertical) and can you do this in the ( W:Any H:Any ) size class or do you have to set constraints for each individual size class?
Thanks!
One size class is all you need in this case.
Set a constraint for each of the 4 gaps between the rects (i.e. the space between the top 2, between the top-left and bottom-left, ...).
Set a constraint for each rect to its 2 closest outer edges.
Now you've set four constraints for each rect - and that's all you need!
For that case: follow the steps
1.Add four uiview to screen, two top and two bottom.
2.In your case the view not touching to bottom layout guide,so you have to give fixed height.
Start the add constraints to views.
All views are correct width and height.
3.So take first view from left top,give leading space ,top space and adjecent spacing to right top view. And select the pin button from the canvas below right side option availabe, select height ,give how much height you need.
4.Now select right top view ,give constraint to top layout guide and trailing space.and also same height what you selected before for view one.
5.Now view three ,left bottom view , give leading ,spacing to top view and right spacing to fourth view and select same height .
6.Now fourth view, give spacing to top view ,trailing to container and select height.
Almost completed.
7.Select all views at once ,give equal withs(option available in pin).
lastly some warning will shows ,do update frames.
I have a picture in the center of my cell, I added a leading and a trailing to center this, it works in a lot of devices but when I try to display this on the iPhone 4 the screen is too small so the length of the leading and trailing are too longs so the picture is compressed and we could not see the image.
So I want to do calculations like leading = screen_width/3 and trailing = screen_width/3
It is possible and it is a good solution ? How to do this or here is an better way ?
When there is such requirement that you want to show View A in centre of View B, I recommend the following constraints:
- View A : Equal Width to superview (i.e. View B), with multiplier. (In your case 1/3 = 0.33).
- View B : Equal Height to superview (i.e. View A), with multiplier. (In your case 1/3 = 0.33).
- View A Center Vertically to superview (i.e. View B).
- View A Center Horizontally to superview (i.e. View B).
If the leading will be w / 3 and trailing is the same. So the image width itself is w / 3 as well. So set Width constraint to be width = superview.width / 3. Then add another constraint to center it horizontally
If you want to centralize your image, you can do it through auto layout and that should work for all the devices. This is the approach I would take-
First take an image view inside your table view cell and with cursor centralize it.
Now-
Step 1: control drag from the image view to you table view's content view.
and select "Center horizontally" and "Center vertically option".
At this point, you may see some red or yellow line but avoid that. We are going to deal with that now.
Step 2:
Pin the top and botton to the content view. Here I assumed your image height is same as your cell height but that can change according to your requirement.
So, pin to the content view like -
Now you are just left with your image's width. In my case I assumed it is going to be same as the height , so I just had to control drag to itself and select "Aspect Ration" like-
This is you final constrains
That should work fine. If your image view's width is not same as your its height then you can you "Equal Width" property with respect to the cell's content view and tell it how you like your width to be.
You DO NOT use trailing or leading space constraints when you are centralize an object inside another.
layouts and i have added 5 button on my view-controller and here i have applied constant width and height for all 5 fields
Here my main requirement is i want to set equal horizontal spacing between all fields and
For this i have fit for constraint For "middle button"
1)topspace
2)width
3)height
4)center horizontal in container
For "fist button"
1)topspace
2)width
3)height
4)leading space
For fifth button:-
1)topspace
2)trailing
3)width
4)height
For second and Fourth buttons i have given 1)top space,width,height,horizontal spacing but they are not setting perfectly please help me screen is coming like first image
but i want fit that screen like second image
Start out by setting each one of your views to have a constant width, let's say that it's 40. So every view now has a width of 40.
Now you need to place a leading/trailing constraint on your furthest left/right view to the superview (This is assuming you want the furthest left/right view to have a static distance from the left/right superview).
Now go ahead and create a horizontal constraint from your furthest left view to the second one in. Create a horizontal constraint from the second to the third. Then make a horizontal constraint from the furthest right view to the fourth. Now the fourth to the third.
What you should now do is edit the four horizontal constraints you have created by changing their value to not being "= constant", but ">= constant". Now you can lower that constant value to help adjust for smaller screen sizes as well. This setup should provide the result that you're looking for by having all of the views equally distanced, while letting the left and right view be closer to the sides.