Ios 如何制作NSConstraint';s长度根据设备尺寸成比例变化

Ios 如何制作NSConstraint';s长度根据设备尺寸成比例变化,ios,iphone,swift,constants,ios10,Ios,Iphone,Swift,Constants,Ios10,我想根据设备大小更改上述约束的长度,以便我的文本始终与屏幕顶部成比例的距离 您可以使用self.view.frame.height 像这样: let height = self.view.frame.size.height let constraint = CGFloat(Double(height) / 3) yourConstraint.constant = constraint 其中,yourConstraint是此约束的@IBOutlet。您可以像添加其他项目一样添加它,如UILab

我想根据设备大小更改上述约束的长度,以便我的文本始终与屏幕顶部成比例的距离


您可以使用
self.view.frame.height

像这样:

let height = self.view.frame.size.height
let constraint = CGFloat(Double(height) / 3)

yourConstraint.constant = constraint
其中,
yourConstraint
是此约束的
@IBOutlet
。您可以像添加其他项目一样添加它,如
UILabel
UIView
。只需将其拖放到代码中


希望它有助于首先为约束创建IBOutlet

让我们看看您的约束名称是myConstraint。现在,如果您希望验证码的距离为顶部的20% 您可以使用以下代码:

var parentFrameHeight = self.view.frame.height
myConstraint.constant = parentFrameHeight*(0.2)

您可以通过在UI本身中调整乘数来增加屏幕高度,从而实现按比例的顶部空间

它将自动调整顶部空间,而无需执行任何出口限制

-> UIView

     -> UILabel (make hidden,for adjusting proportional top space)

         Constraints : top space - 8(default), width - 50(constant any value) , center horizantaly of parent UIView, equal height to parent UIView.
         Now modify your multipliers of proportional height to 0.2 (20% from your main view height) or 0.3(30 %).It will adjust based on screen size automatically.

     - > UILabel (Verification Code) Constraints : top space min 8 (default) & others...

您必须为此创建@IBOutlet。选择约束线,创建NSLayoutConstraint并设置其常量。例如:
@IBOutlet var top:NSLayoutConstraint
top.constant=85
@sadhu检查我的答案这应该可以,谢谢。没有办法通过故事板吗?比如说,你可以使用乘数根据其他视图设置标签的高度。@sadhu_sanjay我从来没有遇到过这个解决方案,但也许你可以。我是这样做的:)@sadhu_sanjay如果我的回答有帮助,请接受它(按寒鸦)。ThanksHow修改等高乘数将影响顶部空间约束(设置为8),您说-UILabel(使隐藏,用于调整比例顶部空间)是什么意思@sadhu_sanjay first UILabel是视图顶部和第二个UILabel之间不可见的中间控件,用于根据屏幕大小调整顶部空间。只需单击第一个标签的高度约束,您就可以在右侧面板中看到倍增属性,将其编辑为0.2或0.3。。,你可以看到adjustments@sadhu_sanjay查看我的图片。别忘了向上投票。这不是我一直在寻找的最干净的解决方案,但这是可行的。感谢接受,投票表决。