【问题标题】:Constrain UIView's proportionally按比例约束 UIView
【发布时间】:2018-04-07 23:09:35
【问题描述】:

我想以相对的方式定位我的观点。我想使用百分比而不是像素。假设我想将按钮的中心定位在其超级视图高度的 25% 处。我该怎么做?

一种方法是:

button.topAnchor.constraint( equalTo: superView.topAnchor, constant: /* compute constant "dynamically") .isActive = true

但这感觉很愚蠢,因为我们在许多 Layout Margin API 函数中都有一个 multiplier 参数。

但是,如果我输入 button.centerYAnchor.constraint,建议的补全都不会显示任何接受 multiplier 参数的函数,除了名称中带有 systemSpacing 的函数。我研究过“系统间距”,但无法弄清楚它的作用。

如果我有这个限制:

button.topAnchor.constraintEqualToSystemSpacingBelow(rootView.topAnchor, multiplier: 0.2).isActive = true

视图正好位于其 superView 顶部的正下方。

【问题讨论】:

    标签: ios autolayout uikit uilayoutguide


    【解决方案1】:

    我还没有找到使用布局锚点的方法,但这里是如何通过直接创建 NSLayoutConstraint 来做到这一点:

    NSLayoutConstraint(item: button, attribute: .centerY,
        relatedBy: .equal, toItem: superView, attribute: .bottom,
        multiplier: 0.25, constant: 0).isActive = true
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      相关资源
      最近更新 更多