不在 set updateConstraints 中时,UIStackView 间距会打破约束

UIStackView spacing breaks constraints when not in set updateConstraints

我有一个简单的 ViewController,带有一个名为 MyView:

的自定义子视图
import UIKit

class ViewController: UIViewController {

    let myView = MyView()

    override func viewDidLoad() {
       super.viewDidLoad()
       myView.translatesAutoresizingMaskIntoConstraints = false
       view.addSubview(myView)
       myView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -8.0).isActive = true
       myView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8.0).isActive = true
       myView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8.0).isActive = true
   }

}

class MyView: UIView {

    let stackView: UIStackView
    let label = UILabel()
    let label2 = UILabel()

    init() {
      stackView = UIStackView(arrangedSubviews: [label, label2])
      super.init(frame: .zero)

      stackView.axis = .vertical

      label.translatesAutoresizingMaskIntoConstraints = false
      label.text = "Label 1"
      label2.translatesAutoresizingMaskIntoConstraints = false
      label2.text = "Label 2"
      stackView.spacing = 8.0        
      addSubview(stackView)
   }

   required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
   }

   override func updateConstraints() {
        super.updateConstraints()
        stackView.fillSuperview()
   }

}

当我 运行 这样做时,我在控制台中得到以下打破约束:

2018-03-23 16:11:58.960493+0100 ViewWithStackView[75612:10756278] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
(
"<NSLayoutConstraint:0x608000280f00 'UISV-canvas-connection' UIStackView:0x7f8438e07510.top == UILabel:0x7f8438d06740'Label 1'.top   (active)>",
"<NSLayoutConstraint:0x608000280fa0 'UISV-canvas-connection' V:[UILabel:0x7f8438e07790'Label 2']-(0)-|   (active, names: '|':UIStackView:0x7f8438e07510 )>",
"<NSLayoutConstraint:0x608000280ff0 'UISV-spacing' V:[UILabel:0x7f8438d06740'Label 1']-(8)-[UILabel:0x7f8438e07790'Label 2']   (active)>",
"<NSLayoutConstraint:0x608000280550 'UIView-Encapsulated-Layout-Height' UIStackView:0x7f8438e07510.height == 0   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x608000280ff0 'UISV-spacing' V:[UILabel:0x7f8438d06740'Label 1']-(8)-[UILabel:0x7f8438e07790'Label 2']   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2018-03-23 16:11:58.961737+0100 ViewWithStackView[75612:10756278] 
    [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
     "<NSAutoresizingMaskLayoutConstraint:0x6080002812c0 h=--& v=--& UIStackView:0x7f8438e07510.height == 0   (active)>",
"<NSLayoutConstraint:0x608000280f00 'UISV-canvas-connection' UIStackView:0x7f8438e07510.top == UILabel:0x7f8438d06740'Label 1'.top   (active)>",
"<NSLayoutConstraint:0x608000280fa0 'UISV-canvas-connection' V:[UILabel:0x7f8438e07790'Label 2']-(0)-|   (active, names: '|':UIStackView:0x7f8438e07510 )>",
"<NSLayoutConstraint:0x608000280ff0 'UISV-spacing' V:[UILabel:0x7f8438d06740'Label 1']-(8)-[UILabel:0x7f8438e07790'Label 2']   (active)>"
)     
Will attempt to recover by breaking constraint 

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

现在,如果我将下面的行 stackView.spacing = 8.0 移动到 updateConstraints 方法,约束将不会中断。因此,以下代码在控制台中没有警告/错误的情况下工作正常:

import UIKit

class ViewController: UIViewController {

    let myView = MyView()

    override func viewDidLoad() {
       super.viewDidLoad()
       myView.translatesAutoresizingMaskIntoConstraints = false
       view.addSubview(myView)
       myView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -8.0).isActive = true
       myView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 8.0).isActive = true
       myView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -8.0).isActive = true
   }

}

class MyView: UIView {

    let stackView: UIStackView
    let label = UILabel()
    let label2 = UILabel()

    init() {
      stackView = UIStackView(arrangedSubviews: [label, label2])
      super.init(frame: .zero)

      stackView.axis = .vertical

      label.translatesAutoresizingMaskIntoConstraints = false
      label.text = "Label 1"
      label2.translatesAutoresizingMaskIntoConstraints = false
      label2.text = "Label 2"        
      addSubview(stackView)
   }

   required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
   }

   override func updateConstraints() {
        super.updateConstraints()
        stackView.fillSuperview()
        stackView.spacing = 8.0
   }

}

为什么将 stackView.spacing = 8.0 移动到 updateConstraints 可以解决这个问题/删除警告/错误?

感谢您的解释:)

在第一种情况下,stackView 的高度为零,因为您尚未为其设置框架(在 fillSuperview() 之前的 init 中),因此在 2 个标签之间设置 8 的间距会引起冲突,但在第二个框架为零,间距为零,根据(内在内容大小),2个标签的高度将为零,因此,在这种情况下不会发生冲突,然后将间距设置为 updateConstraints 但在那一刻 stackView 的框架等于它的 superView 根据这条线 fillSuperview() 所以也很顺利

看起来这是你不想要的约束

"<NSLayoutConstraint:0x608000280550 'UIView-Encapsulated-Layout-Height' UIStackView:0x7f8438e07510.height == 0   (active)>"

我首先要尝试的是

stackView.translatesAutoresizingMaskIntoConstraints = false

此外,我不知道 stackView.fillSuperview() 在做什么,但您最好为其添加约束以填充视图。