简体   繁体   English

如何指定间距约束作为超级视图尺寸的一部分?

[英]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.

相关问题 在Xcode 5中为superview添加间距约束 - Add spacing constraint to superview in Xcode 5 如何创建动态弹出的子视图并扩展其超视图大小。 (在代码中) - How do I create a SubView that pops up dynamically and expands it's superview size. (in code) 如何参考UIView的位置和大小来更新UIView的位置和大小? [迅速] - How can I update a UIView's position and size in reference to it's superview's position and size? [Swift] 如何以编程方式对按钮施加约束(水平间距) - How do I programmatically put constraint to buttons (horizontal spacing) 如何通过自动调整大小使UICollectionView填充其超级视图? - How do I make a UICollectionView fill it's superview with autosizing? 如何在 SwiftUI 中按比例设置高度和宽度? - How to set height and width in proportion with superview in SwiftUI? 我可以在iOS上指定AutoLayout约束以与最近的可见邻居间隔吗? - Can I specify a AutoLayout constraint on iOS for spacing to the nearest visible neighbor? Swift 尾随约束间距取决于父视图的宽度乘以一个值(以编程方式) - Swift trailing constraint spacing depending on width of superview multiplied with a value (programmatically) 如何使我的超级视图调整大小,以使其子视图的大小与Autolayout相匹配? - How do I make my superview resize to match the size of its subviews with Autolayout? 为什么当我相对于超级视图的底部进行约束时,它是从超级视图的顶部进行操作的? 现在正确了吗? - Why when I made my constraint relative to the superview's bottom did it operate from the superview's top? And is it now right?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM