ios - Swift - 约束 - 动态标签宽度

标签 ios swift autolayout

我在一行中有三个按钮。

A - B --- C

我以编程方式添加了这些约束:

A 有约束条件:

leading to superview (value = 16)
width (value = 49)
height (value = 40)
trailing to B (value = 16)
top to top layout guide (value = 16)

B 有约束:

width (value = 49)
height (value = 40)
top to top layout guide (value = 16)

C 有约束:

trailing to superview (value = 16)
height (value = 40)
top to top layout guide (value = 16)
leading to B (value >= 20)

现在,如果我运行应用程序并提示“无法同时满足约束”,C 中的 leading to B 会导致错误。

我认为这是由 C 标签的“缺失”内容(稍后在运行时添加)导致的,因此宽度未知。

我已经尝试手动添加 C:

width (value = 20) 

“错误”仍然存在,但这应该满足导致 B >= 20 的条件。

However, If I run app, even though the error is printed, the UI looks correct. What is the problem / solution for this?

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:0x1c4098ab0 h=--& v=--& UIView:topView.width == 0   (active)>",
    "<NSLayoutConstraint:0x1c4092480 H:[UIView:parentParent]-(0)-|   (active, names: '|':UIView:topView )>",
    "<NSLayoutConstraint:0x1c40924d0 UIVIew:topView.leading == UIView:parentParent.leading   (active)>",
    "<NSLayoutConstraint:0x1c0094140 H:|-(0)-[UIView:parent](LTR)   (active, names: '|':UIView:parentParent )>",
    "<NSLayoutConstraint:0x1c00940a0 UIView:parent.right == UIView:parentParent.right   (active)>",
    "<NSLayoutConstraint:0x1c0094000 H:|-(16)-[UIButton:A]   (active, names: '|':UIView:parent )>",
    "<NSLayoutConstraint:0x1c0093f60 UIButton:A.trailing == UIButton:B.leading - 16   (active)>",
    "<NSLayoutConstraint:0x1c0094730 H:[UIButton:B]-(>=20)-[UIButton:C]   (active)>",
    "<NSLayoutConstraint:0x1c0094780 UIButton:C.trailing == UIView:parent.trailing - 16   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x1c0094730 H:[UIButton:B]-(>=20)-[UIButton:C]   (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.

其中 A、B、C 是按钮,parent 是它们的父 View 。整个父 View 都在 parentParent 中,并且都在 top View 中。

最佳答案

您的 View 具有转换为约束的自动调整大小掩码,请尝试删除它们:

button.translatesAutoresizingMaskIntoConstraints = false

关于ios - Swift - 约束 - 动态标签宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49209883/

相关文章:

ios - 我可以通过明确邀请告诉iOS Gamecenter自动匹配吗?

ios - UIImageView 之间的碰撞检测

swift - 如何更改标签中某些单词的颜色 - Swift 3

ios - 如何在alert.addTextfield中放置$硬币掩码?

ios - 无法更改自定义 View 的框架大小

iOS - 自动布局中心 View 中的多个图像

IOS自动布局约束

ios - TRON 快速响应头

ios - 如何在 flutter 中获取 bundle id

Swift 自动完成 firebase 搜索