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.
Related
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
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
I am developing an iPhone app in which, i have made this page using autolayout
but it shows unwanted spaces above Register Me(yellow button) in bigger iPhones
Constraints given are:
top,bottom, leading and trailing with respect to its subviews to all the controls. (there is no warning or misplaced constraints i see in xcode)
Here is the screenshot of iPhone 4s
Here is the screenshot of iPhone 6+
I want to minimize the empty spacing in bigger iPhones...
How do i solve this problem?
Please help and thanks in advance
Heres a suggestion for you. You have roughly seven regions of interest there. First one is the title 'Register Kano .. ' etc. Then there are 5 text entry areas - name, email, password, etc. Finally you have 3 small lines of info text - I would count this as one final area, so 7 in all.
What I would do is create 7 parent UIViews to put that stuff in. I would give them 'equal height' constraints, and make them sit above and below each other with no gap. Then as the iphone screen changes height, those areas stretch out height-wise to fill the area. Heres a rough mockup :
Select all those UIViews and select the 'equal heights' constraint :
Then every view except the bottom one needs these constraints (top, leading trailing to superview 0px)
Then your bottom view needs those plus 'bottom' too :
So all that remains to do is to put your content in each of those boxes, but centre them in terms of vertical position relative to their superview.
If your intent to target iOS 9 users, you should use UIStackView, the provide a lot of flexibility in terms of alignment and distribution.
If not, then you or your design team has to find a solution.
You can update the margin contraints of your controls on big devices.
This would add space between each rows on big screens, will looks better as you ask.
Something like :
myControl.heightConstraint.constant += 20.0f
This is how I would do, but you have to decide how you update your layout for big devices
#Krunal you can use the size class and set the constraints accordingly so it will give a brief idea how to fit the UI design for every device!!
you can set constraints relative to height of your base view. This will make your interface objects evenly distributed over layout. Lets assume you have 8 regions. for registerLabel
registerLabel.leading = baseView.leading
registerLabel.trailing = baseView.trailing
registerLabel.top = baseView.top
registerLabel.bottom = baseView.bottom * (1/8)// it will be always proportional to height of view
then for icons at the beginning firstIcon
firstIcon.leading = baseView.leading + 10
firstIcon.height = firstIcon.width
firstIcon.height = baseView.height*(1/9)// to give a gap between icons
firstIcon.bottom = baseView.bottom * (2/8)
and then you can set textfield's constraints according to firstIcon
for other icons you can go on with 3/8, 4/8, 5/8, 6/8 and set the relative textfields constraints according to that icon.
and lastly for registerMe button
registerButton.leading = baseView.leading
registerButton.trailing = baseView.trailing
registerButton.bottom = baseView.bottom
registerLabel.height = baseView.height * (1/8)
With this approach your icons size also decrease and increase relatively with height of view and all the page will be filled.
baseView is view of controller.
What is happening here, you kept the width according to superview ie phone width, but your height is fixed.
Do one thing instead of giving Height constraint, use aspect ration in combination with leading , trailing and top constraint. When view will stretch widthwise , your component's height will also Increase and fill the extra space for iphone 6.
This is the best thing you can do keeping the same layout and design pattern.
Seems you have given some fixed height in views.
As every element is depends on it above and below elements constraints, this might be possible of empty space.
So try to divide the View part then the dependency of constraints will reduce.
If you don't want to show empty space, then use center horizontally to one of the element which will look good, here it may be the third field.
Then start giving same constraints to Register button. Sure it will help you in resolving this issue.
Hey its the best solution for you is stackView
Combine your objects in single stackView.
You can combine your object vertically and also horizontally.
It will decrease your space and view will arranged in awesome manner.
You can select your objects like this.
Click here to make it stack.
Now you can reduce your space like this.
I have already found several questions and answers about this topic but I simply cannot get this to work for me.
The blue rectangle is a UIView which contains the Test Label and Gear image button. The red one is a second UIView.
What I'd like to do is to get the blue bar (which is a UIView) increase in size for larger sized screens, which will result in the scaling of the Label and settings button, while the Red UIView shrink in height so the blue UIView can become larger. So basically the question is, how to proportionally scale the UIViews for different screen sizes.
I have tried using aspect ratio constraints, height constraints (greater than/less than) but I just can't get the desired effect.
Thanks.
Try proportional height and width. You need to make constraints for Equal Heights and/or equal Widths and then you double click on the constraint and change the multiplier for example 0.2 if you wish to make First Item to be 20% of second item in width and 2.0 for 200% (means double) in width. to make it increase or decrease proportionally according to your requirement
You have two ways for do that:
1. Proportional constraints:
but if you set a proportional height for one view means that the view change for every little change in screen size. For example, your view can be a little bigger switching from 4" to 4,7", but does not mean that it is a bad idea.
For use that feature just check the answer by Asadullah Ali :)
2. Use size-classes:
if you using Xcode 6 you can use the size classes and add a constraints just for a screen size; it's not difficult to use, check the Apple Documentation.
For this feature you need to able size classes for your Storyboard:
after that you can set up your view controller like you want and add a rule just for one of the classes:
you can tell to Xcode "ok, if the screen size is an 5,5 at least use this rule".
Remember that for add a constraint for a specific size classes you need first select the size class then add normally your constraint: when a size class is selected (you can check in the size classes control) Xcode add the constraints just for that class.
EDIT 1: If you have some trouble with scaling font & similar you can also use size-classes feature just for scaling a font size in a label for example; you find this option by clicking on this after activated the size classes feature:
I prefer tell you everything I know about your problem but in your case personally I would use the first solution: your view can dynamically change whatever is the screen size.
You need just find the best proportional percentage: you can set maybe around 10%, so the 0.1 multiplier with first item your blue view and for second item the red view.
here's what I've been struggling with for the past hours.
I have a view controller that contains a few labels, a picture, and a tableview. I'm using autolayout/size classes in order to use only one storyboard for the various devices supported (meaning: all size of iphones and iPad).
Noew everything is working, except one thing : the settings in "width compact x height compact" are used for 3"5 and 4" iPhones in landscape. So either I say that the table view takes 480px and there's a whole lot of space on iPhones 5/5S, either I set it at 480px but then I cannot reliably align items vertically in my cells.
How come there is no way to say "this view should always takes the full width" ?
Any solution, even workaround, would do. Thanks a lot for your time !
There is no distinction in size classes between 3.5", 4" and 4.7" devices. They all have the same size classes deviced for horizontal and vertical, and behave the same way on rotation.
You can define a view to take all the available space. Explicitly set the bottom and top views to be attached to bottom and top edges (respectively) with constraints, and set the middle view (in your case, the table view) to take all the space in the middle by setting constraints on top with the top views, and constraints on bottom with the bottom views. Now, because top view is attached to top and bottom is attached to bottom, you will have the middle view grow and shrink as needed.
There are a couple of ways to get percentage based sizing.
Link the width of your target element to the width of another element.
Pin your element to its Superview and use the Multiplier setting instead of Constant.
Say you want to set a TitleLabel to have a relative width compared to another element.
Example for method 1:
Ctrl + drag from TitleLabel to another Target element (such as its Parent View), then choose Equal Widths, you can then change the Multiplier field in your Constraints settings to 0.5 if you want 50% width.
Example for method 2:
Pin the appropriate edges of TitleLabel to either the Center, Bottom or Trailing edges* of its Superview (Editor -> Pin -> X Space to Superview).
In the Constraints Settings, make sure you have TitleLabel in the First Item field, and the Superview as the Second Item.
*because the Top/Leading edge of the Superview will have a 0 value, a multiplier against these won't work, as 0 x 0 = 0.
I found method #1 doesn't work well in Table Views, so you may have more luck with #2.