Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proportional distance constraints in auto layout

I am not able to achieve the desired layout of the views with the auto layout in >iOS6.

I have UIView1 and UIView3 which are fixed to the parent view (correspondingly with the TopSpaceToSuperview, BottomSpaceToSuperView and FixedHeight) and they behave as expected when the parent view changes the height.

Which constraints should I specify in the IB for the UIView2 if I want it to maintain the same proportional distance to its siblings (UIView1 and UIView3) when the parent view changes the height? (as displayed in the image)

resizing the parent view

like image 661
Gytis Avatar asked Feb 15 '23 21:02

Gytis


1 Answers

The way to do this is to use invisible "spacer" views between your views.

You can't have relatively sized spaces so use these views instead.

Where the current spaces are place a UIView in each.

Then (in code as you can't do this in IB) set a height constraint between these with the correct multiplier that you want.

i.e.

[NSLayoutConstraint constraintWithItem1:spacer2
                              attribute:NSLayoutAttributeHeight
                               relation:NSLayoutRelationEqual
                                  item2:spacer1
                              attribute:NSLayoutAttributeHeight
                             multiplier:0.5
                               constant:0];

Then make the other views "stick" above and below these spacer views with 0 spacing.

Then you just have to hide these views and the auto layout will take care of the rest.

like image 51
Fogmeister Avatar answered Mar 06 '23 07:03

Fogmeister