【问题标题】:Constraints are broken after pushing VC again再次推送 VC 后约束被打破
【发布时间】:2021-01-10 15:20:21
【问题描述】:

当我弹出 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
    }
    
    
}

【问题讨论】:

    标签: swift uitableview constraints snapkit


    【解决方案1】:

    单元格每次都被重复使用,并且要将模式从 .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()
    }
    

    【讨论】:

      【解决方案2】:

      首先,我建议您将collectionview 用于这种视图。在这部分源码中,你不应该每次都设置约束。顺便说一下,请看下面的链接。 :)

      https://youtu.be/kR9cf_K_9Tk

      【讨论】:

      • 我看了这个视频,我确实喜欢他的节目,但使用 tableView)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-15
      • 1970-01-01
      相关资源
      最近更新 更多