【问题标题】:Can not find constraints created for UIView in constraints property在约束属性中找不到为 UIView 创建的约束
【发布时间】:2018-08-18 07:02:08
【问题描述】:

根据文档,约束实例属性是

视图持有的约束。

var constraints: [NSLayoutConstraint] { get }

但是当我用

创建它们时
  SampleView.rightAnchor.constraint(equalTo: right ,constant: rightConstant).isActive = true 

所以它给我打印了一个约束

<NSLayoutConstraint:0x1c4281540 UIButton:0x103d1d030'Next'.right == UIView:0x103d04740.right   (active)>

但是约束属性什么也没显示。空数组[],但同时 当我定义像 heightAnchor 和 widthAnchor 这样的约束时

   sampleView.heightAnchor.constraint(equalToConstant: 50).isActive = true
   sampleview.widthAnchor.constraint(equalToConstant: 60).isActive = true, and constraints array is not empty 

   [<NSLayoutConstraint:0x1c028bd10 UIButton:0x103d1c5e0'Skip'.height == 50   (active)>,
    <NSLayoutConstraint:0x1c028bf40 UIButton:0x103d1c5e0'Skip'.width == 60   (active)>]

如果有人知道,为什么会这样?

【问题讨论】:

    标签: ios swift layout nslayoutconstraint


    【解决方案1】:

    宽度和高度约束被添加到视图本身,但其他视图的约束被添加到另一个视图,如果它是父视图,如果不是两个子视图之间的共享父视图,请参阅此

    所以在你询问任何视图的限制之前,你应该记住上面的照片

    【讨论】:

      【解决方案2】:

      约束可能在 UIView right 的约束数组中。 heightwidth 约束由视图本身持有,因为它们是仅与 SampleView 本身相关的约束,而右锚约束与 UIView right 相关。

      在 Interface Builder 中处理约束时,层次结构在视觉上更清晰。

      【讨论】:

        猜你喜欢
        • 2017-06-11
        • 1970-01-01
        • 2016-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-17
        • 1970-01-01
        • 2016-08-15
        相关资源
        最近更新 更多