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

时间:2018-03-23 15:24:36

标签: ios swift autolayout nslayoutconstraint uistackview

我有一个简单的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解决此问题/删除警告/错误?

感谢您的解释:)

2 个答案:

答案 0 :(得分:2)

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

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

我要尝试的第一件事是

stackView.translatesAutoresizingMaskIntoConstraints = false

另外,我不知道stackView.fillSuperview()正在做什么,但你可能最好不要添加约束来填充视图。

答案 1 :(得分:0)

在第一种情况下,stackView的高度为零,因为您没有为它设置框架(init之前的fillSuperview()),因此在2个标签之间设置间距为8会导致冲突,但在第二帧中,帧为零,间距为零,根据(内在内容大小),2个标签的高度为零,因此,在这种情况下不会发生冲突,然后在{{1但是在那一刻,stackView根据这一行updateConstraints有一个等于它的superView的框架,所以也顺利进行