gpt4 book ai didi

swift - 我的 "Messaging app"代码中的约束问题

转载 作者:行者123 更新时间:2023-11-28 08:06:59 25 4
gpt4 key购买 nike

我正在尝试制作一个应用程序。它有点像消息传递应用程序,所以我有发件人和收件人。我有一个 collectionView 作为我的主 Controller ,我有一个“ChatMessageCell”类,它是“UICollectionViewCell”类型,还有一个 NSObject 类型的类:“Message”。问题是,当我在我的代码中实现所有这些时,我遇到了很多约束问题(特别是当我尝试发送图像消息时)。

当我第一次发送一条消息像蓝色气泡,然后我发送另一条消息像灰色气泡时,问题就出现了。它向我显示这样的错误:

(此错误示例是当我发送一条蓝色消息(没有错误)然后我发送另一条灰色消息(出现错误))

[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)>

这些是导致错误的约束...聊天消息单元格:

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)

主视图 Controller :

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
}
}

最佳答案

该错误意味着一个布局约束使另一个布局约束无法实现。唯一真正的解决方案是检查约束并找出哪些约束存在冲突以及原因。

附带说明一下,如果您只关心最终结果而不关心手段,那么我强烈建议您查看 JSQMessagesViewController .这是一个开源项目,可以避免尝试手动执行此操作的麻烦。

关于swift - 我的 "Messaging app"代码中的约束问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44814273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com