简体   繁体   English

Swift-约束-动态标签宽度

[英]Swift - constraints - dynamic label width

I have three buttons in one "line". 我在一个“行”中有三个按钮。

A - B --- C A-B --- C

I have programatically added these constraints: 我以编程方式添加了以下约束:

A has constraints: 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 has constraints: B有约束条件:

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

C has constraints: C有约束:

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

Now leading to B in C cause error if I run app and complains on "Unable to simultaneously satisfy constraints." 现在leading to B如果我运行应用程序并抱怨“无法同时满足约束条件”,则导致C中的leading to B导致错误。

I thought that this is caused by "missing" content (it is added later in runtime) of C label and therefore unknown width. 我认为这是由于“缺少” C标签的内容(稍后在运行时添加)导致的,因此宽度未知。

I have tried manually to add for C: 我尝试手动添加C:

width (value = 20) 

The "error" still remains, but this should fulfill the condition for leading to B >= 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.

where A, B, C are presented Buttons, parent is their parent view. 其中A,B,C是按钮, parent是其父视图。 Entire parent view is inside parentParent and all is in top view. 整个父视图在parentParent内部,所有视图都在top视图中。

您的视图已将自动调整大小的蒙版转换为约束,请尝试将其删除:

button.translatesAutoresizingMaskIntoConstraints = false

I have tested your constraints but I don't see errors. 我已经测试了您的约束条件,但没有看到错误。

Unfortunately the console log at runtime is not fun to read, if you use a Xcode >= 7 you can set an identifier to your constraints, select it and go to the "Attributes inspector" like in this image: 不幸的是,运行时的控制台日志不是很有趣,如果您使用Xcode> = 7,则可以为约束设置标识符,选择标识符并转到“属性检查器”,如下图所示:

在此处输入图片说明

After this, your log is more readable. 在此之后,您的日志更具可读性。

If you have problems, you can edit your post with the complete log of the error. 如果遇到问题,可以编辑带有完整错误日志的帖子。

EDIT: 编辑:

The problem would seem with the parent(s) view. 父视图会出现问题。

In this image you can see my test (without errors in the log), I've added only the height constraints to parent and parentParent, you can check the difference. 在此图像中,您可以看到我的测试(日志中没有错误),我仅将高度约束添加到parent和parentParent中,您可以检查差异。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM