[英]How do I specify a spacing constraint as a proportion of the superview's size?
Say I have an image view and I want its width to be equal to half of the superview's width. 假设我有一个图像视图,并且希望其宽度等于超级视图宽度的一半。 I can add an "Equals Width" constraint between the image view and the superview, using a multiplier of 0.5.
我可以使用0.5的乘数在图像视图和超级视图之间添加“等于宽度”约束。
Similarly, I'm looking for a way to specify a spacing constraint as a proportion of the superview's size. 同样,我正在寻找一种方法来指定间距约束作为超级视图大小的一部分。 Continuing with the example above, instead of wanting the width of the image view to be half of the superview's, I would, for instance, want the distance between the bottom of my image view and the bottom layout guide to be a fifth of the superview's height.
继续上面的示例,例如,我不想让图像视图的宽度为超级视图的宽度的一半,而是希望图像视图底部和底部布局指南之间的距离为超级视图的五分之一高度。
Here is one solution that has worked for me in the past. 这是过去对我有用的一种解决方案。 It feels like a bit of an odd way to do it, but it will do the trick.
感觉有点奇怪,但是可以成功。
First, put five hidden views in your superview that are of equal heights. 首先,在您的超级视图中放置五个高度相等的隐藏视图。 Be sure to use constraints to make these expand and contract with their superview.
一定要使用约束使这些扩展和收缩与他们的superview。 Now no matter what size your superview is, you will have these views inside of it that are exactly one fifth of your superview's size.
现在,无论您的超级视图大小是多少,您都将在其中包含这些视图,这些视图恰好是超级视图大小的五分之一。
To use the example from your question, now you can constrain your imageView's size to be proportional to one of those five hidden views. 要使用问题中的示例,现在您可以将imageView的大小限制为与这五个隐藏视图之一成比例。 Then you can constrain it to be centered to one of them so your spacing will also be appropriate for any size.
然后,您可以将其限制为居中于其中之一,以便您的间距也适合任何大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.