在约束 属性 中找不到为 UIView 创建的约束
Can not find constraints created for UIView in constraints property
根据文档,约束实例 属性 是
The constraints held by the view.
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)>]
如果有人知道,为什么会这样?
约束可能在 UIView 的约束数组中 right
。 height
和 width
约束由视图本身持有,因为它们是仅与 SampleView
本身相关的约束,而右锚点约束与 UIView right
.
在 Interface Builder 中处理约束时,层次结构在视觉上更加清晰。
宽度和高度约束被添加到视图本身,但其他视图的约束被添加到另一个视图,如果它是父视图,如果不是两个子视图之间的共享父视图,请参阅此
所以在你询问任何视图的限制之前,你应该记住上面的照片
根据文档,约束实例 属性 是
The constraints held by the view.
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)>]
如果有人知道,为什么会这样?
约束可能在 UIView 的约束数组中 right
。 height
和 width
约束由视图本身持有,因为它们是仅与 SampleView
本身相关的约束,而右锚点约束与 UIView right
.
在 Interface Builder 中处理约束时,层次结构在视觉上更加清晰。
宽度和高度约束被添加到视图本身,但其他视图的约束被添加到另一个视图,如果它是父视图,如果不是两个子视图之间的共享父视图,请参阅此
所以在你询问任何视图的限制之前,你应该记住上面的照片