【问题标题】:How to make a NSConstraint's Length change proportionately according to the device size如何根据设备大小使 NSConstraint 的长度按比例变化
【发布时间】:2017-06-27 07:05:02
【问题描述】:

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

【问题讨论】:

  • 您必须为此创建@IBOutlet。选择您的约束线并创建 NSLayoutConstraint 并设置其常量。例如:@IBOutlet var top : NSLayoutConstraint!top.constant = 85
  • @sadhu 检查我的答案

标签: ios iphone swift constants ios10


【解决方案1】:

您可以通过在 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...

【讨论】:

  • 修改等高乘数如何影响设置为8的顶部空间约束,你说的什么意思——UILabel(隐藏,用于调整比例顶部空间)
  • @sadhu_sanjay 第一个 UILabel 是您的顶部视图和第二个 UILabel 之间的不可见中间控件,可根据屏幕尺寸调整顶部空间。只需单击第一个标签的高度限制,您可以在右侧面板中看到乘数属性,编辑它设置为 0.2 或 0.3..,你可以看到调整
  • @sadhu_sanjay 检查我的图片。不要忘记投票。谢谢
  • 这不是我一直在寻找的最干净的解决方案,但这很有效。谢谢接受,赞成。
【解决方案2】:

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

像这样:

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

yourConstraint.constant = constraint

对于此约束,yourConstraint@IBOutlet。您可以像UILabelUIView 等其他项目一样添加它。只需将其拖放到您的代码中即可。

希望对你有帮助

【讨论】:

  • 这应该可以,谢谢。有没有办法通过故事板?就像您可以使用乘数根据其他视图设置标签的高度一样。
  • @sadhu_sanjay 我从来没有遇到过这个解决方案,但也许你可以。我是这样做的:)
  • @sadhu_sanjay 如果我的回答有帮助,请接受(按寒鸦)。谢谢
【解决方案3】:

首先为约束创建一个 IBOutlet。

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

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

【讨论】:

    猜你喜欢
    • 2015-06-20
    • 1970-01-01
    • 2012-05-06
    • 2012-01-28
    • 2022-08-23
    • 1970-01-01
    • 2016-01-17
    • 2014-07-24
    • 1970-01-01
    相关资源
    最近更新 更多