Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Auto Layout to change spaces equally on resizing

If I have a view in my app with 3 subviews (as an example) with the following spacing between them:

TOP - 40 points - SUBVIEW 1 - 60 points - SUBVIEW 2 - 80 points - SUBVIEW 3 - 80 points - BOTTOM

I understand how I can use auto layout to ensure each of my subviews keeps it's height and width, and I can get everything to be aligned on either a 3.5 OR 4 inch iPhone screen.


But I can't work out what kind of constraint I need to make it so that if it were aligned for a 3.5" screen, and then went to a 4" screen, each spacing would increase proportionally (e.g. 40 points would go to 47, 60 to 71, 80 to 95 - or thereabouts).

Is this possible? Or do I need to make all the spacing equal between the elements? (If so how would I still get it to resize equally?)


I'm new to Auto Layout so if I've missed anything off, or haven't made it clear what I means please let me know, thank you.

like image 444
Jon Cox Avatar asked Jan 07 '13 12:01

Jon Cox


People also ask

What is the use of auto layout in Figma?

Auto layout is a property you can add to frames and components. It lets you create designs that grow to fill or shrink to fit, and reflow as their contents change. This is great when you need to add new layers, accommodate longer text strings, or maintain alignment as your designs evolve.

What are two properties that auto layout constraints control on a Uiview?

Auto Layout defines margins for each view. These margins describe the preferred spacing between the edge of the view and its subviews. You can access the view's margins using either the layoutMargins or layoutMarginsGuide property. The layoutMargins property lets you get and set the margins as a UIEdgeInsets structure.


Video Answer


1 Answers

The Trick is to add not only one but TWO constraints between your views. One with "Greater Than or Equal" and one with "Less Then or Equal". The minimum size (Greater Then or Equal) should be the spacing on the 3.5" display. The maximum size (Less Then or Equal" should be the spacing on the 4" display.

Example:

TOP - 40 points - SUBVIEW 1 - 60 points - SUBVIEW 2 - 80 points - SUBVIEW 3 - 80 points - BOTTOM

TOP - SUBVIEW1: Select both in Interface Builder. Add the constraint "Vertical Spacing" two times. Set one to Greater Then or Equal 40. Set the other to Lesser Then or Equal 47.

The sum of all Greater Then values + all heights of your views should be 480 pixel (3.5") The sum of all Lesser Then values + all heights of your views should be 568 pixel (4")

like image 190
sust86 Avatar answered Oct 11 '22 10:10

sust86