Proportional distance constraints in auto layout

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

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)

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!