gpt4 book ai didi

ios - 按钮 heightConstraint 打破自动布局

转载 作者:行者123 更新时间:2023-11-28 14:08:40 25 4
gpt4 key购买 nike

下面是我的一个 Controller 的用户界面。

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。

你有什么想法吗?

最佳答案

您的纵横比约束表明宽度越大,高度就越大。纵向时,每个按钮的宽度比横向时的宽度小。

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

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

关于ios - 按钮 heightConstraint 打破自动布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52776645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com