【问题标题】:create loop to constraint width and height objects创建循环以约束宽度和高度对象
【发布时间】:2020-08-14 01:52:52
【问题描述】:

我想要做的是创建一个循环,允许 pic 和 slide 具有相同的宽度和高度约束。因为我将 0.25 约束到所有约束。我认为有一种方法可以做到这一点,而不必编写所有 4 行代码。

NSLayoutConstraint.activate([
  pic.heightAnchor.constraint(equalToConstant: 0.25),
  pic.widthAnchor.constraint(equalToConstant: 0.25),

  slide.heightAnchor.constraint(equalToConstant: 0.25),
  slide.widthAnchor.constraint(equalToConstant: 0.25),
])

【问题讨论】:

    标签: swift for-loop height width nslayoutconstraint


    【解决方案1】:

    你可以的

    [pic,slide].forEach {  
        $0.heightAnchor.constraint(equalToConstant: 0.25).isActive = true
        $0.widthAnchor.constraint(equalToConstant: 0.25).isActive = true
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      • 2013-02-16
      • 1970-01-01
      相关资源
      最近更新 更多