不知道如何防止约束破坏

时间:2017-11-06 02:07:49

标签: ios swift xcode autolayout

我是iOS开发的新手。我正在使用自动布局创建一个聊天应用程序,但每当我在我的应用程序中打开collectionview时,我就会受到“打破约束”。 这是我得到的错误

2017-11-06 07:34:52.838630+0530 ChatHub[4083:306096] [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:0x6040002989c0 BubbleView.width == 205.938   (active, names: BubbleView:0x7ffde3611880 )>",
    "<NSLayoutConstraint:0x604000298600 ProfileImageView.width == 32   (active, names: ProfileImageView:0x7ffde3611a60 )>",
    "<NSLayoutConstraint:0x60400028d890 H:|-(8)-[ProfileImageView](LTR)   (active, names: ProfileImageView:0x7ffde3611a60, '|':ChatHub.ChatMessageCell:0x7ffde360e670 )>",
    "<NSLayoutConstraint:0x6040002988d0 H:[ProfileImageView]-(8)-[BubbleView](LTR)   (active, names: BubbleView:0x7ffde3611880, ProfileImageView:0x7ffde3611a60 )>",
    "<NSLayoutConstraint:0x604000298880 BubbleView.right == ChatHub.ChatMessageCell:0x7ffde360e670.right - 8   (active, names: BubbleView:0x7ffde3611880 )>",
    "<NSLayoutConstraint:0x6000004870d0 'UIView-Encapsulated-Layout-Width' ChatHub.ChatMessageCell:0x7ffde360e670.width == 375   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6040002989c0 BubbleView.width == 205.938   (active, names: BubbleView:0x7ffde3611880 )>

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.
2017-11-06 07:34:53.041831+0530 ChatHub[4083:306096] [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:0x600000485c30 BubbleView.width == 205.938   (active, names: BubbleView:0x7ffde345dfb0 )>",
    "<NSLayoutConstraint:0x600000485aa0 ProfileImageView.width == 32   (active, names: ProfileImageView:0x7ffde345e190 )>",
    "<NSLayoutConstraint:0x600000485a00 H:|-(8)-[ProfileImageView](LTR)   (active, names: ProfileImageView:0x7ffde345e190, '|':ChatHub.ChatMessageCell:0x7ffde345b380 )>",
    "<NSLayoutConstraint:0x600000485b40 BubbleView.right == ChatHub.ChatMessageCell:0x7ffde345b380.right - 8   (active, names: BubbleView:0x7ffde345dfb0 )>",
    "<NSLayoutConstraint:0x600000485b90 H:[ProfileImageView]-(8)-[BubbleView](LTR)   (active, names: BubbleView:0x7ffde345dfb0, ProfileImageView:0x7ffde345e190 )>",
    "<NSLayoutConstraint:0x60400029a810 'UIView-Encapsulated-Layout-Width' ChatHub.ChatMessageCell:0x7ffde345b380.width == 375   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600000485c30 BubbleView.width == 205.938   (active, names: BubbleView:0x7ffde345dfb0 )>

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.

我该如何解决这个问题。我被困在这里过去2天..请帮助

enter image description here

  

我在上面的屏幕截图中收到错误,但我没有在下面找到一个

enter image description here

2 个答案:

答案 0 :(得分:1)

据我所知,这个单元格的水平约束看起来像这样。

H:|-8-[ProfileImageView(=32)]-8-[BubbleView(=206)]-8-|

当你的视图宽度等于它的所有子视图和它们周围的空格( 254 )的宽度之和时,你的布局就好了。当细胞变宽(或变窄)时,麻烦就开始了。

在这里,您可以看到您配置的宽度与单元格的封装宽度(为375 )冲突。

<NSLayoutConstraint:0x6000004870d0 'UIView-Encapsulated-Layout-Width' ChatHub.ChatMessageCell:0x7ffde360e670.width == 375   (active)>

如果要保持轮廓图像的宽度并修复气泡视图。我建议您将约束符号(BubbleView.right)设置为大于或等于。

<NSLayoutConstraint:0x604000298880 BubbleView.right == ChatHub.ChatMessageCell:0x7ffde360e670.right - 8   (active, names: BubbleView:0x7ffde3611880 )>

另一个选择是确保单元格的宽度始终等于254,这有点难以实现。

答案 1 :(得分:0)

根据错误信息,您似乎并排创建struct complex{ double real; double imaginary; }; BubbleView。自动布局约束是

ProfileImageView

请注意,只要确定了单元格的宽度,就可以(left edge)| - (8) - Profile(32) - (8) -- Bubble -- (8)| (cell right edge) 自动计算该气泡大小。因此,气泡宽度的约束是多余的。只需删除它。