AutoLayout dynamic spacing? - ios

I'm pretty new to the whole autolayout design and I'm trying to figure out the best way to make this design compatible on all sizes. I can't seem to find a way to space out each element so the size of each element changes according to the screen size. I know that I can constrain the subviews to the margin but Xcode seems to require a set width and height so the larger screen version ends up looking extremely compressed. (see below) What would be the best way to handle a custom form like this? Basically I need a way to dynamically space out each element.
Each element is on its own. IE text,dividers,icons

If I understood your issue correctly then I think!you need to set leading,top,tailing and bottom space and aspect ratio if necessary for change in height of textfield to the view of which you need to increase or decrease size according to its width.

Related

Autolayout, several UILabels, lines in each, same font size?

I have a number of UILabels on a view and a varying amount of text in each. I need the font size to be the same in each.
I’m using autolayout to position / size the labels.
However some of the “g” characters are being cut off.
I’m not sure how I can resolve this ?
change the lines property of the lables to 0 and hook then properly to each other or to the superView so when there content is large they wrap smoothly
You cannot control font size directly via auto-layout constraints. You can disable adjusting font on all labels and this way they will have same label.
But to better help answering your question (and underlaying issue you're trying to solve) - it would be much better if you share screenshot with your interface.
SizeToFit fixes my issue, rather than autolayout

How to use auto layout so my app fits all screen sizes in swift?

I need my buttons to stay in the same positions for all phones.
I want it to look like this on all iPhones:
But when I switch to a larger size phone it does this, it also looks ugly on smaller phones as well:
That is a VERY broad question, as a lot of iOS UI depends on autolayout. I suggest you work through these tuts at at least AutoLayout. From your screen shots, it looks as if you have set a width constraint on your view with the green background instead of pinning the leading, trailing, top, and bottom to the edges.
Auto layout is, in essence, a system that performs calculations based on constraints. This means that to perform what you want, you need to add constraints to each of your objects so that the compiler knows how you want to resize your UI. But like others have said, that is a very general question. I suggest looking into the topic a bit before asking, so you can narrow down your question and get better answers.
You need to define positions for all your button or views. You need to define atleast 4 constraints for all your view. By adding constraints you tell your views where they should be placed on all screens. You define their positioning with respect to screen by adding constraints.
When you add trailing, from top and width and height constraint you tell that view should be placed in fixed position i.e. x , y and occupy defined space whatever may be screen size.You can opt for fixed width or height. Instead of fixing height and width you can also define leading or trailing constraints for the view. Constraints will adjust views frame according to screen size.
For beginning you can opt for some tutorials available online. You can check Raywanderlich here. Hope it helps.

Xcode: constraining varying height UILabels to fixed size container

I’m building an iOS app which includes a quiz. Questions are displayed in the upper portion of the screen (see below). There are always five possible answers. The thing is: the answers are procedurally generated and vary in length, which leads to line breaks in the label sometimes.
This is the current state
Maybe it’s hard to tell from the picture, but the spacing between the first and second and then between the second and third is not the same as I intend it to be.
This is what I had in mind
Essentially, I’d like the top- and bottommost labels to have the same space towards the container. The labels in the middle should all have the same spacing between each other, but should also adapt, if one of the labels gets bigger (when the size of a text is longer than the width and a line break occurs).
To achieve this, I tried the following:
Organise the labels as a stack view:
Nearly worked, the only problem I have here is, that the size of the
labels is calculated after the stack view is displayed, which leads to wrong constraints/paddings/margins being applied to the (possibly) longer texts at runtime.
Organise the labels with regular constraints
I tried setting the
priority of the constraints for the middle labels lower than
that of the top- and bottommost ones, so those would be the ones
to resize, if the labels enlarges, but it appears that at
runtime one of those is chosen to shrink, whereas the others
remain at their default size.
I'd really appreciate if you could help me out, constraints always seem to be a pain in my neck...
Agreed... I think UIStackView works fine, just have to set it up properly.
Also you may need to call .sizeToFit on the labels when you set the text.
I put up an example for you - the sizing is not exact, but you should be able to follow the technique...
https://github.com/DonMag/StackViewFun
I think the best practice in your case would indeed be to use a UIStackView. Then you have multiple option in solving this problem:
You can set the distribution of the UIStackView to Fill Proportionally and set a Minimum Font Size or Minimum Font Scale to your UILabels.
You can also set the distribution of the UIStackView to Fill and then manually specify the height of your containers. The UILabels should still have some Minimum Font Size or Minimum Font Scale.
EDIT:
I just realized you want to keep all UILabels to have the same font sizes. Then a possible solution would be to embed the UIStackView into a UIScrollView and constraint the UIStackView's Leading, Trailing, Top and Bottom constraints to the ones of the ContentView of the UIScrollView. In this way the height of the UIScrollView will adapt in accordance of the needed height by the UIStackView.
Hope this will help you!

Autoresizing UI components using Autolayout iOS8

I have designed a screen for a portrait only iPhone app. I have applied Autolayout so that the view horizontally fits all the screen sizes. The problem is, for smaller screen sizes, the content is truncated vertically. I tried not to set a fix height of components but it breaks all the alignment of the components.
Please see the image for reference;
Any quick suggestion for what I'm missing...
Thanks
If I were you I would would follow one of two approach -
you can use spacer views for spacing between components so that will adjust spacing on the basis of screen height
2.or if your content is bigger than iphone screen then you can always opt for scroll view.(I prefer pure layout approach)
Set vertical spacing between components. Don't fix width and height of components. set aspect ratio constraint for every component.
Like many of the comments and answers above, there are many ways to solve this problem. The decision for you to take is which is more suitable
Aspect Ratio Scaling : One of the common ways to retain the same UI across all form factors is to use the aspect scaling techniques in which constraints are given to mimic the aspect scaling.
Flexible Spacing and Sizing rules : Another way to design the layout is to design the layout to have different types of spacing based on the form-factor of the device. This way layouts may not to be consistent across.
Using Size classes for iOS8 and Above these can be used maintain various layouts for all the form-factors.
Use of ScrollView scrollView can be used if you want to retain the original layout and have scroll to view the layouts that are out of bounds.

Should I lay out every element with auto-constraints?

Auto contraints seems like a bit of a chore. I have a bunch of elements in my main view (this is a single-view app) inside Main.storyboard. So imagine a few labels, buttons, controls, etc.
For example, take one label that says "Hello World" that's centered horizontally in the upper portion of the screen.
I need to add the following constraints to make it appear "normal" in my various devices ipad/iphone:
Horizontal Center constraint
Leading Space
Top Space
Trailing Space
It works, but it seems like a lot of work, I don't remember doing this work in the good old days (~3 years ago) with struts and springs. So I need to add all these constraints to each of my other elements too. So if I have 10 elements, I need roughly 10x4=40 contraints? Is this the ideal way to do this?
If it were up to me a view and the elements as I place them in the view and all controls in it would just be stretched relatively to fit the size of the device. An example would be like in photoshop, where we just decide what the overall image looks like and then scale the image to just fit the dimensions that we want. Is this possible in xcode?
Note that my app is just portait only - for iphone/ipad, but looks exactly the same in both (ie same layout for the controls)
You don't need to set that many constraints.
Horizontal center + top space would be enough given your example, or leading, trailing and top space.
If you are setting leading, trailing and horizontal center, you are over-specifying in that axis and possibly giving the auto-layout system contradictory information that will cause problems.
For that specific example: no, you only need a horizontal center and a top space, because UILabels have an intrinsic content size so their width and height is specified by their content and not constraints (unless you want them to be).
You didn't do this work in the good old days because Auto Layout wasn't on iOS then and there was only one particular possible iPhone size at the time: now there are 4. Auto Layout is an essential tool for modern iOS development.
As for controls stretching relatively to the device size, it depends on the control, but in general standard controls maintain the 44 point touch target rule - for example nav bars have gotten wider to accommodate wider phones but they haven't gotten any taller, because the larger screen size should be used to display more content instead of more chrome.
But yes, if you wanted to have controls have particular dimensions based on device size that is definitely possible with Auto Layout.

Resources