简体   繁体   English

何时在iOS AutoLayout中使用乘数?

[英]When to use Multiplier in iOS AutoLayout?

An autolayout constraint can be definded as: 自动布局约束可以定义为:

self.view.addConstraint(NSLayoutConstraint(item: label, 
       attribute: .Bottom, 
       relatedBy: .Equal, 
       toItem: self.view, 
       attribute: .Bottom, 
       multiplier: 1, 
       constant: 0))

I did not understand what a use case would be for using the multiplier . 我不了解使用multiplier的用例是什么。

When to use Multiplier in iOS AutoLayout? 何时在iOS AutoLayout中使用乘数?

One use case that I commonly use is when I want one view to be 30% of the width of another view. 我通常使用的一种用例是,当我希望一个视图占另一视图宽度的30%时。 It would look like this: 它看起来像这样:

self.view.addConstraint(NSLayoutConstraint(item: label, 
   attribute: .Width, 
   relatedBy: .Equal, 
   toItem: self.view, 
   attribute: .Width, 
   multiplier: 0.3, 
   constant: 0))

If you have two view. 如果您有两个视图。 and your requirement is that one view is always half of other view or may be 10% of other view than you can use multiplier. 并且您的要求是,一个视图始终只能是其他视图的一半,或者可能是其他视图(不能使用乘数)的10%。

for ex, 例如

在此处输入图片说明

i want this two view always in this proposition. 我希望这两种观点总是在这个命题中。

在此处输入图片说明

so drag and drop from one view to another and select equal height. 因此从一个视图拖放到另一个视图,然后选择相等的高度。

bt it shows errors that both view not in same height. bt它显示两个视图不在同一高度的错误。

so select height 所以选择身高 在此处输入图片说明

now you have to change multiplier by calculating like (second_view_height/first_view_height). 现在您必须通过计算(second_view_height / first_view_height)来更改乘数。 and add that in multiplier 并将其乘以

在此处输入图片说明

this way you use in storyboard 这样您在情节提要中使用

for manual coding you can use this way 对于手动编码,您可以使用这种方式

self.view.addConstraint(NSLayoutConstraint(item: label, 
   attribute: .Width, 
   relatedBy: .Equal, 
   toItem: self.view, 
   attribute: .Width, 
   multiplier: 0.4869, 
   constant: 0))

In Apple documentation : 在Apple文档中:

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. 除1以外的任何值都会创建比例约束。

Take a look at this link : Auto Layout Multiplier 看一下这个链接: 自动布局倍增器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM