Autolayout - how to distribute things to proportionally fill screen [duplicate] - ios

This question already has answers here:
Springs in Auto Layout: Distribute views evenly, with constraints, in Xcode 5
(2 answers)
Closed 4 years ago.
In my app, I have four square buttons that I want to proportionally change size for each different screen size that adhere to the margin constraints set. However, I am unable to set a proportional margin size between the trailing edge of the view controller and the leading part of a button, it requires a hard number. How do I get around this. Because i want the app to fit all screen sizes I want the autolayout to measure everything in terms of screen proportions but this doesn't seem possible.
Any advice on how to get around this issue would be much appreciated!
Thank you

I see you want margins and sizes of the buttons to be relative to screen size.
I recommend you to set in Interface Builder only margins around the buttons for each size class. If the number of the buttons is fixed, probably this will be the easiest way to solve you problem.

Related

How to do this custom auto layout? [duplicate]

This question already has answers here:
Springs in Auto Layout: Distribute views evenly, with constraints, in Xcode 5
(2 answers)
Closed 5 years ago.
I am trying to get this layout to work but have failed in these past two days trying.
All the labels have a constant height. All squares on the left are images of icons that are not important.
My problem is regarding the Y axis. This is a frame of an iPhone SE. I need it to expand accordingly to bigger screens like iPhone 8 Plus and iPhone X. Similarly, I could design it in Interface Builder for iPhone X and it should shrink reasonably until iPhone 4s. As far as my understanding goes, size classes don't work on iPhone - iPhone variation since they are all compact width and regular height.
These vertical lines highlighted in blue are not visible in the real app. They are an attempt to always center the black horizontal lines between two UILabel frames. So, what I really need is: if the screen gets smaller, the blue vertical lines should shrink. If the screen gets larger, the blue vertical lines should expand. The problem arises when I try to also make the UITableView have a variable height.
Anyone has any ideas to how I could solve this problem? It's very important to me.
There are many ways to achieve what you're asking for, but UIStackView is great for these kind of things. To answer your question in detail would take too long, but here's a screenshot of a quick mockup in interface builder, perhaps you can figure it out.
I didn't quite understand what you meant by those blue lines, in my example the tableview height is dynamic as its hugging priority is lower than other elements. But you could give desired height to each element and set stackview distribution property to "fill equally", then the stack view will fit your screen in y axis and will create equal spacing between elements.
and the simulator screenshot:
And here I gave each container view a static height and set the main stack view distribution to "equal spacing", as you can see it adjusts the spacing between the container views:
If you want them to be the same proportion depending on your screen, you can make them with multiplier:
Try make them the same height as your container view, but with multiplier 0.15, or something. If they equal, you need one constraint for 1:0.15 and all others to be equal to this one

Constraints for universal storyboard on iPhones

I'm struggling a bit with constraints on iOS with the differents screens sizes nowadays.
I tried google and stackoverflow to find a solution but my english does not seem fluent enough to find an answer.
I got 4 buttons verticaly aligned, the first and the last one are constrained by the edges of screens (kinda easy). But I'm really struggling with the constrains of the 2 middle buttons. I can't find a way to make them equally spaced from the left and right buttons on every size screens.
Is there something easy and tricky to make these constraints right ? Or am I doing it wrong and should I try to do it programmaticaly ?
Thanks
Add three invisible views in middle of each button, make their width equal with each other with constraint then your four buttons will be equally spaced. For the Y then you just need to pin it at your desired place.
If you are going to support only iOS 9 and higher, then use a UIStackView.
The solutions to this problem is very simple. This can be solved using the concept of "spacer views". You have to place invisible views between each red coloured view. You would be requiring 3 in your case. Then make their background as clear colour. .
Next, make their width equal and constrain their edges to the views that are after and before that view. You then define the size for red coloured view.
REMEMBER dont give the "clear coloured views" any fixed width. It would be determined by the runtime.This would solve the problem. Tell me if any more information is required.
Here is a blog post for the solution for this
http://adamdelong.com/fluid-layouts-with-auto-layout-size-classes-spacer-views-and-constraint-priorities/
This is the youtube video for this
https://www.youtube.com/watch?v=eSG-3-QpmWk&feature=youtu.be
Besides Tj3n's answer with views between buttons, you could use
A UIStackView (iOS 9!) where you use for settings Axis: horizontal, Distribution: equal spacing
A Toolbar (depends on what you want to do with your buttons) with toolbar items and flexible Space between them
Why not size classes? Apple introduced the concept of adaptive user interfaces in iOS 8 relying on a combination of Auto Layout and size classes.
If you aren't aware of what is size classes, there are plenty of tutorials available, please find one.
Summary: Apple very cleverly removed two story borads for iphone and ipad and made a single story board for universal app. No you dont have to struggle trying to apply autolayout constraint that satisfies all the screen sizes :)
Below are few of the size classes and their meaning :)
Regular width x Regular Height ----> iPad Potrait mode/ipad landscape mode
Compact width x Regular Height ----> iPhone 6 plus,iPhone 6,iPhone 5s,iPhone 5,iPhone 4s potrait mode
Compact width X Compact Height ----> iPhone 6,iPhone 5s,iPhone 5,iPhone 4s landscape mode
Regular width x Compact Height ----> iphone 6 plus landscape mode.
You can select the size classes you want to support from story board :) and start applying constraints specific to each size classes (like buttons in middle) or if you have generic one (like your buttons fixed to screen) for all the size classes.
You can deploy, remove, reuse or delete the constraints form various size classes.
SUMMARY: Buddy, If you are not using size class yet, its a high time to start using it :) There is a wonderful video on it in apple WWDC sessions 2014 i believe. Download, watch, start playing with it.
Happy coding :)
you can use equations to get this appearance.
use views' trailing points to get this.
View1.trailing = superview.trailing*(2.0f/9.0f)
View2.trailing = superview.trailing*(4.0f/9.0f)
View3.trailing = superview.trailing*(6.0f/9.0f)
View4.trailing = superview.trailing*(8.0f/9.0f)
if you make View1.width = superview.width*(1/9.0f) you can achieve what you want.
there are a lot of similar solutions for this issue. but the base is using trailing points.
It may also be done with using centerX positions of Views.
View1.centerX = superview.trailing*(3.0f/15.0f)
View2.centerX = superview.trailing*(6.0f/15.0f)
View3.centerX = superview.trailing*(9.0f/15.0f)
View4.centerX = superview.trailing*(12.0f/15.0f)
Thanks to LearneriOS answer, I solved my problem.
In order to get my wished result, I created 3 views with 10 width.
My first and my last button were already constrained. I constrained my first extra view to my first button with Horizontal spacing then i constrained my first extra view with the Center vertically in container. I then constrained my extra view to his own width and heights.
There come the important part: I did go on the constraint menus and selected the width constraint. The value inside was still 10 but I did change the priority from 1000 to 750.
Then I did copy my first extra view and constrained all of them to the nearest buttons, the same way I did with my first entra view but I removed their width constraint (to all the extra views but the first one) and constrained the extra views with the first one by plugging the: Equals Width.
Then I got my result, I hope it was clear enough and thanks again.

Not understanding auto layout, constraints and size classes

I'm a bit stuck and any help would be greatly appreciated!
I'll give you a quick overview. I have designs for a screen that were built in sketch using an iphone 6 screen size, then redesigned to fit and look right for an iphone 5/5s/5c, and 6 plus as those are the only devices that I want to support.
But the design portion or implementing the designs for just one specific screen size is easy and I understand that part. Where am I'm lost completely, is how to implement a design in one view controller that looks the way that it should in all the screen sizes I designed for.
I've gone through more than a few auto layout and size class tutorials and not sure how I properly use them so that the app recognizes "this is an iphone 5, use these image sizing and placements instead, and this is an iphone 6 plus, use these" and so on.
Everything I've seen to this point regarding auto layout and constraints only use 1 set of numbers to judge distance from elements for example, but all screen sizes would have different distances.
What am I missing or not understanding? I know I'm looking at something improperly.
Thanks in advance for all help!
You're correct that just one set of numbers could be used to judge distances, but this can still describe how a view should appear on different screen sizes. Your problem may be that you're thinking of constraints as describing the frame of your view? (Which obviously has to be different on every device). I find it more helpful to think of constraints as describing how each edge of my view relates to another view.
For example here's a view controller I setup with all the same constraints and how it would look on different devices. The constraints describe how the large grey view is pinned it's left and right edges 20 points from the left and right edges of its container view. It's pinned to 8 points from the top and 8 points from the top of the label. The label is centred vertically and horizontally and it has intrinsic content size. Each button is pinned 20 points from the bottom edge with button 1 and 2 being pinned to the 20 point from the left and right edges respectively.
I don't know if that helped or if that wasn't the answer you were after and you need to arrange your views differently depending on the device: you can tell Xcode which device size and orientation the constraints you're creating are for, using this button in Interface Builder.
Use it to select a device size/orientation. After, any constraints you create will only be used on that device. By default any width and height are selected so normally your constraints are applied to all devices.
Also, you cannot choose which devices you want to support, only the iOS version.

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.

How to scale the UI for iOs 5+

I have built a UI for iPhone 6 using the storyboard.
NOTE: Autolayouts are enabled
The UI elements are positioned with constant constraints relative to other elements.
When i run the app on the simulator for iPhone 6 it works completely fine, the elements are positioned correctly.
The issue happens when i run the app on iPhone 6 plus and iPhone 5
On iPhone 6 and iPhone 5 the constraints have constant values and the UI gets distorted due to the change in the screen size.
I have gone through the raywenderlich blog for autolayouts
How do i scale the current UI up/down based on the screen size?(if there is a way), since they share the same aspect ratio?
EDIT:
I have hosted a small demo here. I would like to build a single UI and scale it across the form factors.
Here I've attached a project with the constraints setup. There's probably plenty of ways to set them up to get what you want. This is just one way to do it.
Hopefully this link will work.
This link is broken as of today 7 April 2016 http://speedy.sh/zPEcq/ConstraintsPlayground.zip
There's actually some constraints that's not needed in there aswell. (Forgot to remove them) They are "greyed out", and are under the green and yellow views. You can just delete them.
Answer to comment:
To "scale" the distances you need to define what you should scale from. Is it the width of the screen? What scale should it be? Should there be a max distance? a min distance? You can do almost anything with constraints, but it's sometimes a real mess due to xcode being a b*tch.
Here's an example of how you can scale the distance between two views according to the size of the screen. The gray views are just spacing views, because you need to make aspect ratio from width to width for the spacing view and the container view. The spacing views should have clear color in production:
This link is broken as of today 7 April 2016 http://speedy.sh/B7R95/ConstraintsPlayground-2.zip
The UI elements are positioned with constant constraints relative to other elements.
In that case it should be sufficient to not give the UI elements a constant width and height. In that case iOS will stretch the UI elements to fulfill the constant constraints between the different UI elements.
How do i scale the current UI up/down based on the screen size?(if there is a way), since they share the same aspect ratio?
You can set constraints to preserve the aspect ration of your elements but I don't think this is what you are looking for. Generally you don't need to think about aspect ratios. Just think about how your UI should behave if the screen is wider or taller.
As Spoek already pointed out it would be very useful to have access to your storyboard to figure out the problem. If you don't want to hand out your storyboard you may want to update your question with screenshots and a description of what constraints you put in place.

Resources