我正在尝试制作应用。它就像一个消息应用程序,所以我有发件人和收件人。我有一个collectionView作为我的主控制器,我有一个“ChatMessageCell”类,类型为“UICollectionViewCell”,我也有一个类型NSObject:“消息”。问题是当我在我的代码中实现所有这些时,我遇到了很多约束的麻烦(特别是当我尝试发送图像消息时)。
当我第一次发送蓝色气泡这样的消息时出现问题,然后我发送另一条消息,如灰色气泡。它向我显示如下错误:
(这个错误的例子是当我发送一条蓝色的消息(没有错),然后我发送另一个像灰色的那样(出现错误))
[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:0x600000289790 UIView:0x7fbeead23880.width == 136.117 (active)>",
"<NSLayoutConstraint:0x6000002896a0 UIView:0x7fbeead23880.right == practica_say_whaat.ChatMessageCell:0x7fbeead43770.right - 8 (active)>",
"<NSLayoutConstraint:0x6000002892e0 H:|-(8)-[UIView:0x7fbeead23880](LTR) (active, names: '|':practica_say_whaat.ChatMessageCell:0x7fbeead43770 )>",
"<NSLayoutConstraint:0x60000028b540 'UIView-Encapsulated-Layout-Width' practica_say_whaat.ChatMessageCell:0x7fbeead43770.width == 375 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600000289790 UIView:0x7fbeead23880.width == 136.117 (active)>
这些是造成错误的约束...... ChatMessageCell:
var bubbleWidthAnchor: NSLayoutConstraint?
var bubbleViewRightAnchor: NSLayoutConstraint?
var bubbleViewLeftAnchor: NSLayoutConstraint?
override init(frame: CGRect) {
super.init(frame: frame)
addSubview(bubbleView)
addSubview(textView)
//This is the bubble of the message (here is an error)
bubbleViewRightAnchor = bubbleView.rightAnchor.constraint(equalTo: self.rightAnchor, constant: -8)
bubbleView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
bubbleWidthAnchor = bubbleView.widthAnchor.constraint(equalToConstant: 200)
bubbleWidthAnchor?.isActive = true
bubbleView.heightAnchor.constraint(equalTo: self.heightAnchor).isActive = true
bubbleViewLeftAnchor = bubbleView.leftAnchor.constraint(equalTo: self.leftAnchor, constant: 8)
HomeViewController:
var messages = [Message]()
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellId", for: indexPath) as! ChatMessageCell
let message = messages[indexPath.item]
cell.textView.text = message.text
setupCell(cell, message: message)
if let text = message.text {
//a text message
cell.bubbleWidthAnchor?.constant = estimateFrameForText(text).width + 32
cell.textView.isHidden = false
} else if message.text == nil {
//fall in here if its an image message
cell.bubbleWidthAnchor?.constant = 200
cell.textView.isHidden = true
}
return cell
}
//In this function, i think is the other error connected to the other one)
fileprivate func setupCell(_ cell: ChatMessageCell, message: Message) {
if message.id == 1 {
//outgoing blue
cell.bubbleView.backgroundColor = ChatMessageCell.blueColor
cell.textView.textColor = UIColor.white
cell.bubbleViewRightAnchor?.isActive = true
cell.bubbleViewLeftAnchor?.isActive = false
cell.messageImageView.isHidden = true
} else if message.id == 2 {
//incoming gray
cell.bubbleView.backgroundColor = UIColor(red: 240/255, green: 240/255, blue: 240/255, alpha: 1.0)
cell.textView.textColor = UIColor.black
cell.bubbleViewRightAnchor?.isActive = false
cell.bubbleViewLeftAnchor?.isActive = true
cell.messageImageView.isHidden = true
}
else if message.id == 3 {
cell.messageImageView.image = imagenSeleccionada
cell.messageImageView.isHidden = false
cell.bubbleView.backgroundColor = UIColor.clear
}
}
答案 0 :(得分:0)
错误意味着一个布局约束使另一个布局约束变得不可能。唯一真正的解决方案是通过约束并找出哪些约束是冲突的以及为什么。
作为旁注,如果你只关心最终结果而不是手段,那么我强烈建议你看JSQMessagesViewController。这是一个开源项目,可以省去尝试手动执行此操作的麻烦。