【问题标题】:What is the difference between Top and TopMargin in NSLayoutAttribute?NSLayoutAttribute 中的 Top 和 TopMargin 有什么区别?
【发布时间】:2015-03-31 11:07:57
【问题描述】:

我正在尝试通过代码创建约束:

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

但是,我不确定哪个 NSLayoutAttribute 适合此约束。 image 应该与主 Superview self.view 有一个顶部空间。

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

对于图像 A,我应该使用 NSLayoutAttribute.Top 还是 NSLayoutAttribute.TopMargin

【问题讨论】:

  • 感谢您的评论!这个问题稍微解释了一些事情,但没有回答我的问题。
  • 我通常不使用边距约束,这是个人喜好,要么约束到视图的边缘,要么约束到它的边距(8px)。假设您的图像 A 的顶部为 8px,您可以创建一个常量为 8 的顶部约束,或者一个常量为 0 的顶部边距约束,您将得到相同的结果。
  • 谢谢!所以你是说创建 0px 的上边距约束等于创建 8 px 的上边距约束?
  • 可以,只要边距保持 8px

标签: ios swift autolayout constraints size-classes


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 2010-10-02
    • 2011-12-12
    • 2010-09-16
    相关资源
    最近更新 更多