大小类和堆栈视图轴约束冲突

时间:2016-03-16 11:08:44

标签: ios interface-builder constraints size-classes uistackview

我正在尝试一些非常简单的事情,而且我不理解由此产生的约束冲突。让我解释一下:

我有一个漂亮的视图控制器,全屏垂直UIStackView(约束设置为0到顶部,前导,尾部和底部),里面有两个视图。

顶视图作为约束高度200pt,底视图没有约束。由于堆栈视图具有Fill分配模式,一切都很好。

现在,我希望堆栈视图在iPhone上横向切换到水平轴。所以,在我的故事板上,我选择w 任意 | h 紧凑尺寸类,卸载高度约束,添加宽度约束,然后选择水平轴......

行为正常,不幸的是,它检测到我无法解释的约束冲突:

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:0x7fba62c1b7f0 H:[UIStackView:0x7fba62c0a3c0]-(0)-|   (Names: '|':UIView:0x7fba62c0bd10 )>",
    "<NSLayoutConstraint:0x7fba62c1b890 H:|-(0)-[UIStackView:0x7fba62c0a3c0]   (Names: '|':UIView:0x7fba62c0bd10 )>",
    "<NSLayoutConstraint:0x7fba62c1ba80 H:[UIView:0x7fba62c15420(200)]>",
    "<NSLayoutConstraint:0x7fba62d1c230 'UISV-canvas-connection' UIStackView:0x7fba62c0a3c0.leading == UIView:0x7fba62c15420.leading>",
    "<NSLayoutConstraint:0x7fba62dcad80 'UISV-canvas-connection' H:[UIView:0x7fba62c15420]-(0)-|   (Names: '|':UIStackView:0x7fba62c0a3c0 )>",
    "<NSLayoutConstraint:0x7fba62dc69d0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7fba62c0bd10(736)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x7fba62c1ba80 H:[UIView:0x7fba62c15420(200)]>

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.

所以,如果有人能告诉我什么是错的!

1 个答案:

答案 0 :(得分:0)

遇到这个问题,它似乎可能是UIStackView中的一个错误,其中来自前一个/默认大小类的内部生成的约束是挥之不去的。这个解决方法(Swift)适合我:

override func traitCollectionDidChange(previousTraitCollection: UITraitCollection?) {
  super.traitCollectionDidChange(previousTraitCollection)

  self.setNeedsLayout()
  self.layoutIfNeeded()
}