NSLayoutAttribute中Top和TopMargin有什么区别?

Ces*_*are 5 constraints ios autolayout swift size-classes

我试图通过代码创建约束:

constraintImageCharacterTop = NSLayoutConstraint (item: image,
        attribute: NSLayoutAttribute.Top,
        relatedBy: NSLayoutRelation.Equal,
        toItem: self.view,
        attribute: NSLayoutAttribute.Top,
        multiplier: 1,
        constant: viewTop)
    self.view.addConstraint(constraintImageCharacterTop)
Run Code Online (Sandbox Code Playgroud)

但是,我不确定NSLayoutAttribute这种约束的权利是什么.image应该有主要Superview的顶层空间self.view.

这是我认为它的工作方式,但我不确定我是否正确:

Imgur

我应该使用NSLayoutAttribute.TopNSLayoutAttribute.TopMargin用于图像A吗?

小智 6

我通常不使用约束到边距,这是个人偏好,要么约束到视图的边缘,要么约束到它的边距(8px).假设您的图像A的顶部为8px,您可以创建一个常量为8的顶部约束,或者一个常量为0的上边距约束,您将获得相同的结果.