基于条件添加约束的奇怪行为 [英] Strange behaviour on adding constraints based upon condition

查看:67
本文介绍了基于条件添加约束的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为两个人之间的对话做准备。我在自定义单元格中使用了 UICollectionView 并使用了一个标志,基于该标志我决定了消息应向左对齐还是向右对齐。如果我仅使用单个对齐方式,则约束将正常工作,甚至在基于条件的对齐方式中也将正常运行,除非 UICollectionView 中的消息数少于四个。所以我知道约束没有错。但是,一旦添加第四条消息,先前单元格内容和单元格的对齐方式和顺序就会受到干扰。随附的屏幕截图显示了问题。

I am making a view for conversation between two people. I have used UICollectionView with custom cell and using a flag based upon which I decide whether the message should be aligned towards left or right. The constraints work correctly if I use only a single direction of alignment, it even works correctly with condition based alignment unless number of messages in UICollectionView is less than four. So I know that constraints are not wrong. But as soon as I add the fourth message the alignment and sequence of previous cell contents and cells gets disturbed. Attached screenshot shows the problem.

使用3条或更少信息的正确行为

添加了4条或更多消息后,错误的行为 b

wrong behaviour when 4 or more messages added

var avatarHorizontal = NSLayoutConstraint(item: messageCell.avatarImageView,
                                                      attribute: .leading,
                                                      relatedBy: .equal,
                                                      toItem: cell,
                                                      attribute: .leading,
                                                      multiplier: 1,
                                                      constant: 7)
var messageTextContainerHorizontal = NSLayoutConstraint(item: messageCell.messageTextContainer,
                                                                    attribute: .leading,
                                                                    relatedBy: .equal,
                                                                    toItem: messageCell.avatarImageView,
                                                                    attribute: .trailing,
                                                                    multiplier: 1,
                                                                    constant: 10)

if isCellFromUser {
    avatarHorizontal = NSLayoutConstraint(item: messageCell.avatarImageView,
                                                       attribute: .trailing,
                                                       relatedBy: .equal,
                                                       toItem: cell,
                                                       attribute: .trailing,
                                                       multiplier: 1,
                                                       constant: -16)
    messageTextContainerHorizontal = NSLayoutConstraint(item: messageCell.messageTextContainer,
                                                                     attribute: .trailing,
                                                                     relatedBy: .equal,
                                                                     toItem: messageCell.avatarImageView,
                                                                     attribute: .leading,
                                                                     multiplier: 1,
                                                                     constant: -10)
}

更新1:
我尝试过@Sh_Khan的建议,但从此开始就产生了问题收集视图的第一个单元格。

UPDATE 1: I have tried @Sh_Khan's suggestion but that starts making problem from the very first cell of the collection view.

我什至尝试了以下代码,但仍保留了我以前面对的原始格式的问题:

I have tried even the following code and it retains the problem in the original form I was facing previously:

if isCellFromUser {
    cell?.addConstraints([avatarRightAligned, messageTextContainerRightAligned])
} else {
    cell?.addConstraints([avatarLeftAligned, messageTextContainerLeftAligned])
}

所以我注意到了一种趋势当我介绍 if / else 语句时,无论我将引用变量重新用于约束对象的条件赋值还是有条件地添加约束(都有声明的单独参考变量),都会产生问题

So I have noticed a trend that as soon as I introduce the if/else statement, it makes problem no matter I reuse the reference variable for conditional assigning of constraints object or I add the constraint conditionally (having declared separate reference variables to the constraint object).

更新2:
因此,事实证明我必须在<$中添加所有约束c $ c> init 方法而不是 cellForRowAt 方法,然后激活和取消激活 cellForRowAt 内的相关约束。感谢@Sh_Khan抓住并提供了解决方案。现在,出现了另一个问题,就是我必须根据完全相同的条件来更改背景和前景色,因此该解决方案可能是什么?

UPDATE 2: So it turned out that I had to add all the constraints in init method and not in cellForRowAt method and then activate and de-activate the relevant constraints inside cellForRowAt. Thanks to @Sh_Khan for the catch and providing the solution. Now, another problem arises, which is that I have to change background and foreground colors based on the exact same condition so what could be the solution for this one?

UPDATE 3:
我针对 UPDATE 2 在此处

推荐答案


它甚至可以在基于条件的对齐方式下正常工作,除非UICollectionView中的消息数少于四个。

It even works correctly with condition based alignment unless number of messages in UICollectionView is less than four.

收集单元格已出队,因此当第四个单元格出现时,它可能具有左/右约束对齐方式,并且您的代码试图将其设置为相反,因此发生冲突

This happens as collection cells are dequeued so when the fourth cell appears it may have a left/right constraint alignment and your code tries to give it the opposite , hence a conflict

您可以要做的是创建2个约束并将其设置在 init

What you can do is to create 2 constraints and set them inside init

var leftCon,rightCon:NSLayoutConstraint!

然后根据 cellForRowAt

leftCon.isActive = isLeftAligned
rightCon.isActive = !isLeftAligned

这篇关于基于条件添加约束的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆