按钮heightConstraint破坏自动布局

时间:2018-10-12 09:37:33

标签: ios swift autolayout

下面是我的一个控制器的用户界面。

controllerUI enter image description here

如您在屏幕底部看到的(纵向),我有两个带有图像的UIButton。这两个按钮放置在带有.fillEqually分布的水平UIStackView内。这些按钮具有heightConstraint,以保持正确的aspectRatio。 heightConstraint是:

leftButton.heightAnchor.constraint(equalTo: leftButton.widthAnchor, multiplier: 0.75)
rightButton.heightAnchor.constraint(equalTo: rightButton.widthAnchor, multiplier: 0.75)

当设备为纵向并旋转为横向时,一切正常,但是当设备再次旋转为纵向时,我得到以下堆栈跟踪:

2018-10-12 12:08:16.643313+0300 Music for Kids[5823:114100] [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:0x600003e996d0 UIStackView:0x7f96a6e338a0.top == UILayoutGuide:0x6000024ca760'UIViewSafeAreaLayoutGuide'.top   (active)>",
"<NSLayoutConstraint:0x600003e99720 UIStackView:0x7f96a6e338a0.leading == UILayoutGuide:0x6000024ca760'UIViewSafeAreaLayoutGuide'.leading   (active)>",
"<NSLayoutConstraint:0x600003e99770 UIStackView:0x7f96a6e338a0.bottom == UILayoutGuide:0x6000024ca760'UIViewSafeAreaLayoutGuide'.bottom   (active)>",
"<NSLayoutConstraint:0x600003e997c0 UIStackView:0x7f96a6e338a0.trailing == UILayoutGuide:0x6000024ca760'UIViewSafeAreaLayoutGuide'.trailing   (active)>",
"<NSLayoutConstraint:0x600003e999f0 H:|-(0)-[UIView:0x7f96a6e33c80]   (active, names: '|':UIView:0x7f96a6e062c0 )>",
"<NSLayoutConstraint:0x600003e99a90 UIView:0x7f96a6e33c80.trailing == UIView:0x7f96a6e062c0.trailing   (active)>",
"<NSLayoutConstraint:0x600003e9a0d0 UIButton:0x7f96a6e1f320.height == 0.75*UIButton:0x7f96a6e1f320.width   (active)>",
"<NSLayoutConstraint:0x600003e9a170 UIImageView:0x7f96a6e348a0.width == 0.95*UIView:0x7f96a6e33c80.width   (active)>",
"<NSLayoutConstraint:0x600003e9c8c0 'UISV-alignment' UIView:0x7f96a6e062c0.bottom == UIStackView:0x7f96a6e336a0.bottom   (active)>",
"<NSLayoutConstraint:0x600003e9e9e0 'UISV-alignment' UIView:0x7f96a6e062c0.top == UIStackView:0x7f96a6e336a0.top   (active)>",
"<NSLayoutConstraint:0x600003eb77a0 'UISV-canvas-connection' UIStackView:0x7f96a6e336a0.top == UIButton:0x7f96a6e1f320.top   (active)>",
"<NSLayoutConstraint:0x600003eb5270 'UISV-canvas-connection' V:[UIButton:0x7f96a6e2d200]-(0)-|   (active, names: '|':UIStackView:0x7f96a6e336a0 )>",
"<NSLayoutConstraint:0x600003eb3660 'UISV-canvas-connection' UIStackView:0x7f96a6e336a0.leading == UIButton:0x7f96a6e1f320.leading   (active)>",
"<NSLayoutConstraint:0x600003eb3610 'UISV-canvas-connection' H:[UIButton:0x7f96a6e1f320]-(0)-|   (active, names: '|':UIStackView:0x7f96a6e336a0 )>",
"<NSLayoutConstraint:0x600003e80640 'UISV-canvas-connection' UIStackView:0x7f96a6e338a0.leading == UIView:0x7f96a6e062c0.leading   (active)>",
"<NSLayoutConstraint:0x600003e881e0 'UISV-canvas-connection' H:[UIStackView:0x7f96a6e336a0]-(0)-|   (active, names: '|':UIStackView:0x7f96a6e338a0 )>",
"<NSLayoutConstraint:0x600003e9caa0 'UISV-canvas-connection' UIStackView:0x7f96a6e338a0.top == UIView:0x7f96a6e062c0.top   (active)>",
"<NSLayoutConstraint:0x600003e9cd70 'UISV-canvas-connection' V:[UIView:0x7f96a6e062c0]-(0)-|   (active, names: '|':UIStackView:0x7f96a6e338a0 )>",
"<NSLayoutConstraint:0x600003eac910 'UISV-fill-equally' UIButton:0x7f96a6e2d200.height == UIButton:0x7f96a6e1f320.height   (active)>",
"<NSLayoutConstraint:0x600003eb31b0 'UISV-spacing' H:[UIView:0x7f96a6e062c0]-(8)-[UIStackView:0x7f96a6e336a0]   (active)>",
"<NSLayoutConstraint:0x600003eb4460 'UISV-spacing' V:[UIButton:0x7f96a6e1f320]-(8)-[UIButton:0x7f96a6e2d200]   (active)>",
"<NSLayoutConstraint:0x600003eb67b0 'UIView-Encapsulated-Layout-Height' UIView:0x7f96a6e2dd90.height == 812   (active)>",
"<NSLayoutConstraint:0x600003eb4b40 'UIView-Encapsulated-Layout-Width' UIView:0x7f96a6e2dd90.width == 375   (active)>",
"<NSLayoutConstraint:0x600003e99590 'UIViewSafeAreaLayoutGuide-bottom' V:[UILayoutGuide:0x6000024ca760'UIViewSafeAreaLayoutGuide']-(34)-|   (active, names: '|':UIView:0x7f96a6e2dd90 )>",
"<NSLayoutConstraint:0x600003e99540 'UIViewSafeAreaLayoutGuide-left' H:|-(0)-[UILayoutGuide:0x6000024ca760'UIViewSafeAreaLayoutGuide'](LTR)   (active, names: '|':UIView:0x7f96a6e2dd90 )>",
"<NSLayoutConstraint:0x600003e995e0 'UIViewSafeAreaLayoutGuide-right' H:[UILayoutGuide:0x6000024ca760'UIViewSafeAreaLayoutGuide']-(0)-|(LTR)   (active, names: '|':UIView:0x7f96a6e2dd90 )>",
"<NSLayoutConstraint:0x600003e994f0 'UIViewSafeAreaLayoutGuide-top' V:|-(88)-[UILayoutGuide:0x6000024ca760'UIViewSafeAreaLayoutGuide']   (active, names: '|':UIView:0x7f96a6e2dd90 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600003e9a0d0 UIButton:0x7f96a6e1f320.height == 0.75*UIButton:0x7f96a6e1f320.width   (active)> 

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

据我了解,按钮的heightConstraint导致此错误!如果我不设置heightConstraint,则不会出错,但是我需要按照行程顺序来保持正确的AspectRatio。

您有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您的宽高比约束表明宽度更大,高度更高。纵向时,与横向时相比,每个按钮的宽度都较小。

现在,由于在风景中存在更大的宽度时,布局系统也会尝试增加高度。但是在风景中,您没有足够的空间扩展到所需的高度。这打破了高度限制。

我认为您可以通过降低高度限制的优先级来解决此问题,这样,当没有足够的空间来增加高度时,它将停止而不会产生可怕的日志。