【问题标题】:Constraint to make width equal to another约束使宽度等于另一个
【发布时间】:2017-10-08 23:03:46
【问题描述】:

我遇到了一个问题,我希望视图 (indicatorView) 的宽度等于按钮。我正在使用Snapkit,但我认为这个问题也应该与裸 NSLayoutConstraint 相关。这就是我要使indicatorView's 宽度等于button 的方法。

indicatorView.width.equalTo(button.snp.width)

但是,这会使按钮的宽度等于指示器视图的宽度,而不是使指示器视图的宽度等于按钮的宽度。

如何指定关系的方向?这可能吗?

【问题讨论】:

  • 你指的是ActivityIndicatorView吗?
  • 这样的约束总是双向的。它只是说这两个项目必须具有相同的宽度。您必须通过宽度约束或绑定其边缘为您的 UIButton 设置显式宽度。

标签: ios swift autolayout nslayoutconstraint snapkit


【解决方案1】:
indicatorView.translatesAutoresizingMaskIntoConstraints = false
indicatorView.widthAnchor.constraint(equalToConstant: button.widthAnchor).isActive = true

【讨论】:

    【解决方案2】:
    let f = indicatorView.frame
    indicatorView.frame = CGRect(x: f.origin.x, y: f.origin.y: width: button.snp.width, height: f.height)
    

    【讨论】:

      【解决方案3】:

      您是否将其包装在正确的闭包中?所以它应该看起来像:

      indicatorView.snp.makeConstraints { (make) in
              make.width.equalTo(button)
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-16
        • 2017-04-19
        • 2020-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多