Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autolayout - equal distribution of 6 views

I want to have 6 objects (buttons) laid out inside one view. They should, however, follow some constraints:

enter image description here

  • Two top buttons should have the same vertical distance from superview (A)
  • Two bottom - the same (C)
  • Two in the middle should have their centers at the superview's center line
  • The vertical distances between all buttons (E) should be the same
  • and last but not least - the buttons should be square (so the width and height should be the same)
  • A = C
  • B = D

Is it possible to have this effect just in the IB, or should I use some additional code for the constraints?

like image 497
kender Avatar asked Oct 11 '13 20:10

kender


People also ask

What is a stack view?

Stack views are a powerful tool for quickly and easily designing your user interfaces. Their attributes allow a high degree of control over how they lay out their arranged views. You can augment these settings with additional, custom constraints; however, this increases the layout's complexity.

How do you distribute the space between the views in storyboard without using any code?

Just select the views you want to contain in the Interface Builder and choose Editor -> Embed In -> Stack view. Set the appropriate width/height/margin constraints for the stack view, and make sure to set the Distribution property to 'Equal spacing':

What is multiplier in Autolayout?

Multiplier is there for creating Proportional Constraint. Auto Layout calculates the first item's attribute to be the product of the second item's attribute and this multiplier . Any value other than 1 creates a proportional constraint.

What is a stack view iOS?

Overview. Stack views let you leverage the power of Auto Layout, creating user interfaces that can dynamically adapt to the device's orientation, screen size, and any changes in the available space. The stack view manages the layout of all the views in its arrangedSubviews property.


2 Answers

This is a logical request, but constraints are defined using the attributes of views, but cannot not be defined in relation to other constraints. That having been said, there are a number of approaches:

  1. Layout guides: An approach which doesn't require predetermining the any spacing is to have UILayoutGuide objects or, if using iOS versions before 9, just use hidden views, i.e. views with clear background or alpha of zero, in between the buttons.

    The idea is to add these layout guides with addLayoutGuide (or add invisible views with addSubview if supporting iOS versions predating iOS 9) in between your six buttons as "spacers", and define the spacers to be the same size as each other, and with constraints between the spacers, the superview, and the buttons that will go in between the spacer. Once you lay that out (showing the horizontal spacer views in blue, vertical ones in red, just so you can see them):

    spacer views

    The equivalent VFL for the constraints for those red UIView objects, called vspacerX, would be:

    H:|[vspacer1][button1(100)][vspacer2(==vspacer1)][button2(==button1)][vspacer3(==vspacer1)]|
    H:|[vspacer1][button3(==button1)][vspacer2][button4(==button1)][vspacer3]|
    H:|[vspacer1][button5(==button1)][vspacer2][button6(==button1)][vspacer3]|
    

    And constraints on the blue UIView objects, called hspacerX, like:

    V:|[hspacer1][button1(100)][hspacer2(==hspacer1)][button3(==button1)][hspacer3(==hspacer1)][button5(==button1)][hspacer4(==hspacer1)]|
    V:|[hspacer1][button2(==button1)][hspacer2][button4(==button1)][hspacer3][button6(==button1)][hspacer4]|
    

    You don't have to use VFL to define these constraints, as any way you define these constraints will work, but it's just a concise format for describing the collection of constraints that I employed.

    Anyway, when the view is rendered with those layout guides (or invisible views), it yields evenly spaced buttons like so:

    spacers hidden

  2. Another approach is to have six "container" views, that would look like:

    containers

    The equivalent VFL for these six container UIView objects might look like:

    H:|[container1][container2(==container1)]|
    H:|[container3(==container1)][container4(==container1)]|
    H:|[container5(==container1)][container6(==container1)]|
    
    V:|[container1][container3(==container1)][container5(==container1)]|
    V:|[container2(==container1)][container4(==container1)][container6(==container1)]|
    

    You can then add your buttons to that, centering one on each of the six little containers and then make your containers clear:

    just buttons

    This works, too, but just a slightly different spacing (where the margins are half of the spacing between the views, whereas the other approach keeps the margins the same as the spacing between them.

  3. Stack view: In a permutation of the prior point, in iOS 9, you can also use UIStackView, designed precisely for evenly spacing views. In this case, put two buttons each in three horizontal stack views, and then place those stack views within a vertical stack view. This achieves six evenly sized container views.

    See WWDC 2015 video What's New in Cocoa Touch.

    The problem with stack views is that they can be used to ensure even spacing between the arranged subviews, they don't ensure spacing before the first arranged view nor after the last arranged view. So, the kludge to get around that is to, for horizontal stack view, include two more zero width views (or zero height for vertical stack views). Then when you use even spacing on the stack view, it also give you what will appear to be spacing before and after all of the arranged subviews.

  4. NSLayoutAttributeCenterX with multiple: Another technique involves defining the attribute:NSLayoutAttributeCenterX and attribute:NSLayoutAttributeCenterY attributes for your six buttons, but rather than using the constant values, use the multiplier field. This technique enjoys a little simplicity, but doesn't always render the desired effect, so I won't describe it unless it's something you definitely want to pursue. I've already entered tl:dr territory here.

  5. Collection view: Another approach is to use a UICollectionView, which handles this scenario gracefully. It's well designed to let you layout cells in a grid.

  6. Hardcoding values: For the sake of completeness, I'll note that you could simply specify specific values for A, B, C, and D (as well as the width and height constraints). You don't even have to worry about setting the E constraints, but rather just set the vertical center constraint of the middle two to their superview, and you're effectively done (because the spacing represented by E should be a natural result of the previous steps, assuming A=C and B=D). If you want to adjust these values on the basis of device size and/or orientation, you can then implement a viewWillLayoutSubviews to adjust the constants for these constraints according to the size of the view.

like image 139
Rob Avatar answered Oct 25 '22 13:10

Rob


Update: I have a better solution that does not use spacers. Check it out here.

Ok, this can be achieved very quickly in IB. It's so so simple. Here's a diagram that will help illustrate. enter image description here

Assume v1-6 are your buttons, and s1-5 are your spacers. 1) in IB control drag out all of the connections shown by the red lines.

2) shift click v1-6 and pin icon (looks like |-I-| ) set the width and height to a definite value. also, set the height and width to be equal.

3) shift select s1-4 (not 5) and set the height to equal. do not give it a definite height, since this should be calculated by the system. you might also need to set the widths of s1-4 to be equal, but don't give them a definite width.

4) control drag from the centre views to the leading and trailing edge and set the centre constraint.

So, you might think, ok, this should work now. It doesn't. Here's my app running in portrait with slightly different colors. Looks good. (Notice, you would make the spacers invisible once you get it setup).

enter image description here

But when I rotate, oops!

enter image description here

What's happening here? The problem is incredibly easy to solve once we understand what's gone wrong. What we want is for IB to not shrink our views. We want IB to make the spacers and the spaces to shrink and grow as necessary, but to leave our views alone. Basically, IB has shrunk the spacers down as far as it can in portrait and to attempt to make everything fit IB has shrunk our views. But we wanted IB to shrink the vertical spaces between views and spacers, not our views. The solution is so easy. All we have to do is adjust the priority of the vertical spaces and all is well. So, select the vertical spaces in IB and adjust the priority to 750. The vertical spacing lines will show as dashed. Done.

enter image description here

enter image description here

Ok, so here's everything as we expect it.

enter image description here

And with the spacers made clear:

enter image description here

enter image description here

like image 21
smileBot Avatar answered Oct 25 '22 15:10

smileBot