简体   繁体   English

自动布局约束第一项和第二项如何工作?

[英]How Autolayout constraint First Item and Second Item works?

If I have UIView with top 100 left 120 right 120 how exactly First Item and Second Item relationship work.如果我有顶部 100 左 120 右 120 的 UIView,第一项和第二项关系究竟是如何工作的。 After going through Apple document each relationship has a linear equation with an expression as.通过 Apple 文档后,每个关系都有一个线性方程,表达式为。

firstItem.firstItemAttribute == secondItem.secondItemAttribute * multiplier + constant

With this equation for top leading constraint with relation用这个方程来计算与关系的顶部前导约束

  • First Item Safe Area.Leading第一项安全区。领先
  • Relation =关系=
  • Second Item = MyView.Leading第二项 = MyView.Leading
  • Constant 120常数120

with equation the First Item And Second Item I understood.等式是我理解的第一项和第二项。 But Problem for me in understanding when I do the reverse the second item constant changes to - 120 and layout has no effect after the change.但是当我做相反的操作时,我理解的问题是第二项常量更改为- 120并且更改后布局没有任何影响。 Why negative has no effect and what is its use for?为什么否定没有效果,它的用途是什么? Xcode provides a set of properties for FirstItem & SecondItem like leading, center, trailing when we need to switch this properties.当我们需要切换这些属性时, Xcode为 FirstItem 和 SecondItem 提供了一组属性,例如领先、居中、尾随。

  • First Item = Safe Area.Leading --- that's the "left edge" of the Safe Area First Item = Safe Area.Leading --- 这是安全区的“左边缘”
  • Second Item = MyView.Leading --- that's the "left edge" of your view第二项 = MyView.Leading --- 那是你视图的“左边缘”

So, "left edge" of the Safe Area to "left edge" of your view equals 120 , or, your view is 120 pts from the Safe Area (to the right).因此,安全区域“左边缘”到视图的“左边缘”等于120 ,或者,您的视图距离安全区域(右侧) 120 pts。

  • First Item = MyView.Leading --- that's the "left edge" of your view First Item = MyView.Leading --- 这是视图的“左边缘”
  • Second Item = Safe Area.Leading --- that's the "left edge" of the Safe Area Second Item = Safe Area.Leading --- 这是安全区的“左边缘”

So, "left edge" of your view to "left edge" of the Safe Area equals -120 , or, the left edge of Safe Area is -120 pts from your view (to the left).因此,您视图“左边缘”到安全区域的“左边缘”等于-120 ,或者,安全区域的左边缘距离您的视图(向左)是-120 pts。

If you change -120 to 120 , that would put the left edge of the Safe Area +120 pts, or 120 pts to the right, of your view's left edge, which would push the view off the screen to the left.如果您将-120更改为120 ,则会将安全区域的左边缘+120 pts 或向右 120 pts,您的视图的左边缘,这会将视图从屏幕推向左侧。

Edit:编辑:

For a little more clarity...为了更清楚一点......

With "SafeArea.Leading -> myView.Leading", you are saying "put myView's left edge 120-pts from the left edge of the Safe Area"使用“SafeArea.Leading -> myView.Leading”,您是说“将 myView 的左边缘放置在距离安全区域左边缘 120 pts 处”

When you swap them to "myView.Leading -> SafeArea.Leading", you are saying "put SafeArea's left edge minus 120-pts from the left edge of myView" .当您将它们交换为“myView.Leading -> SafeArea.Leading”时,您是在说“将 SafeArea 的左边缘从 myView 的左边缘减去120 点”


In general, when using Interface Builder, you leave the order alone... because you are visually laying out your elements, and IB knows how to define the constraints.通常,在使用 Interface Builder 时,您不必理会顺序……因为您在视觉上布置元素,而 IB 知道如何定义约束。

More often where you will see a difference, is when setting up constraints in code.在更多情况下,您会看到不同之处,是在代码中设置约束时。

For example, to put a subview (red) inside a containing view (blue), with 20-pts padding on all four sides like this:例如,要将子视图(红色)放在包含视图(蓝色)中,在所有四个边上填充 20 pts,如下所示:

在此处输入图片说明

your code might look like this:您的代码可能如下所示:

    let myContainerView = UIView()
    myContainerView.translatesAutoresizingMaskIntoConstraints = false
    myContainerView.backgroundColor = .blue

    // add container view to self view
    view.addSubview(myContainerView)

    // constrain container view center X and Y, width and height both 240-pts
    NSLayoutConstraint.activate([

        myContainerView.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: 0.0),
        myContainerView.centerYAnchor.constraint(equalTo: view.centerYAnchor, constant: 0.0),
        myContainerView.widthAnchor.constraint(equalToConstant: 240.0),
        myContainerView.heightAnchor.constraint(equalToConstant: 240.0),

        ])

    let myInnerView = UIView()
    myInnerView.translatesAutoresizingMaskIntoConstraints = false
    myInnerView.backgroundColor = .red

    // add inner view to container view
    myContainerView.addSubview(myInnerView)

    // constrain inner view with 20-pts padding on all four sides
    NSLayoutConstraint.activate([

        // top and left are 20-pts from superview top and left
        myInnerView.topAnchor.constraint(equalTo:
            myContainerView.topAnchor, constant: 20.0),
        myInnerView.leadingAnchor.constraint(equalTo:
            myContainerView.leadingAnchor, constant: 20.0),

        // bottom and right are *minus* 20-pts from superview bottom and right
        myInnerView.bottomAnchor.constraint(equalTo:
            myContainerView.bottomAnchor, constant: -20.0),
        myInnerView.trailingAnchor.constraint(equalTo:
            myContainerView.trailingAnchor, constant: -20.0),

        ])

You will note that the trailing and bottom constraints of the inner view must be negative, because you want them to be 20-pts away from the trailing and bottom edges of the container view.您会注意到内部视图的尾部底部约束必须为负数,因为您希望它们与容器视图的尾部和底部边缘相距20 磅。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM