如何解决再次推送VC后约束被打破
弹出VC时,我的约束被打破了。我有表视图,当我第一次推送表视图时,约束是完美运行的,但是在再次弹出并推送后,约束被打破了。 第一个图像工作正常,但是第二个图像坏了也许问题出在didSet中,在打破约束后,xcode说要删除宽度,但是ui会很糟糕 这是我的手机代码
var isIncoming: Bool! {
didSet {
bubbleBackgroundView.backgroundColor = isIncoming ? Constants.Color.inComingMessages : Constants.Color.outComingMessages
messageLabel.textColor = isIncoming ? .black : .white
if isIncoming {
bubbleBackgroundView.snp.makeConstraints { (make) in
make.leading.equalTo(16)
make.width.equalTo(180)
}
messageLabel.snp.makeConstraints { (make) in
make.leading.equalTo(16)
}
addSubview(inComingImgTail)
inComingImgTail.snp.makeConstraints { (make) in
make.trailing.equalTo(bubbleBackgroundView.snp.leading).offset(12)
make.top.equalTo(bubbleBackgroundView.snp.top).offset(1)
make.width.equalTo(15)
make.height.equalTo(36)
}
}
else if !isIncoming{
bubbleBackgroundView.snp.makeConstraints { (make) in
make.trailing.equalTo(self).offset(-16)
make.width.equalTo(180)
}
messageLabel.snp.makeConstraints { (make) in
make.leading.equalTo(16)
make.trailing.equalTo(-16)
}
addSubview(OutComingImgTail)
OutComingImgTail.snp.makeConstraints { (make) in
make.trailing.equalTo(bubbleBackgroundView.snp.trailing).offset(4)
make.top.equalTo(bubbleBackgroundView.snp.top).offset(1)
make.width.equalTo(15)
make.height.equalTo(36)
}
}
}
}
这是我的数据源和委托代码
extension ChatVC: UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return ChatVC.messagesList.count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: MessagesCell.reuseIdentifier,for: indexPath) as! MessagesCell
let messages = ChatVC.messagesList[indexPath.row]
let sorted = ChatVC.messagesList.sorted { (first,second) -> Bool in
return first.pk < second.pk
}
cell.messagesLists = messages
cell.messageLabel.text = sorted[indexPath.row].text
if sorted[indexPath.row].from_user == userID {
cell.isIncoming = false
}
else if sorted[indexPath.row].from_user != userID {
cell.isIncoming = true
}
return cell
}
}
解决方法
单元每次都被重用,并且要将模式从.sent切换到.received,您必须停用先前的约束。为这两种状态添加两个约束数组,然后停用前一个数组并激活新的约束。 在您的情况下,您只需附加约束,但不要删除约束
var receivedConstraints = [NSLayoutConstraint]()
var sentConstraints = [NSLayoutConstraint]()
...
final func changeCellMode(to mode: SNKMessage.Mode,didChangeMessageMode: (Bool) -> ()) {
if let _previousMode = previousMessageMode,_previousMode == mode {
didChangeMessageMode(false)
messageBackground.setNeedsDisplay()
return
}
didChangeMessageMode(true)
if mode == .outgoing {
NSLayoutConstraint.deactivate(receivedConstraints)
NSLayoutConstraint.activate(sentConstraints)
} else {
NSLayoutConstraint.deactivate(sentConstraints)
NSLayoutConstraint.activate(receivedConstraints)
}
messageBackground.messageMode = mode
previousMessageMode = mode
setNeedsLayout()
}
,
首先,我建议您对此类视图使用collectionview。在源代码的这一部分中,您不应每次都设置约束。顺便说一下,请看下面的链接。 :)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。