当我在集合视图中滚动时,我的自定义单元格中的UIView锚会不断变化

问题描述

我有一个消息传递应用程序,显示的消息位于集合视图中,我有一个自定义单元,其中有一个显示消息的文本视图,然后将文本视图放在UIView内,这样我就可以给出消息了背景颜色取决于发送邮件用户

我在自定义单元格中的功能将背景视图设置为右侧还是左侧,具体取决于 消息是否来自当前用户

当我使用Firestore时,如果消息是来自当前用户还是UID的其他用户,我会通过在消息模型中上传信息来实现

发生了什么情况,当我有足够的消息可以滚动集合视图时,约束条件开始随机更改,并且某些单元格背景视图扩大了集合的整个宽度

我尝试将函数放在dispatchQueue中,但仍然遇到相同的问题

任何想法?

class MessageCell: UICollectionViewCell {
    
    var bubbleLeftAnchor : NSLayoutConstraint!
    var bubbleRightAnchor: NSLayoutConstraint!
    
    var messagess : Message? {
        didSet {
                configure()
        }
    }
    

    let label : UITextView = {
        let lbl = UITextView()
        lbl.backgroundColor = .clear
        lbl.font = .systemFont(ofSize: 16)
        lbl.textColor = .black
        lbl.isEditable = false
        lbl.isScrollEnabled = false
        return lbl
    }()
    
    var bubble : UIView = {
        let view = UIView()
        view.layer.cornerRadius = 5
        return view
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: .zero)
        
        
        addSubview(bubble)
        bubble.addSubview(label)
        
        bubble.anchor(top: topAnchor,bottom: bottomAnchor,paddingTop: 10,paddingLeft: 50,paddingBottom: 20)
        bubble.widthAnchor.constraint(lessthanorEqualToConstant: 250).isActive = true
        
        
        label.anchor(top: bubble.topAnchor,left: bubble.leftAnchor,bottom: bubble.bottomAnchor,right: bubble.rightAnchor,paddingTop: 4,paddingLeft: 4,paddingBottom: 4,paddingRight: 4)
        
        
        
    }
    
        
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func configure() {
        
        label.text  =  messagess?.message
        bubble.backgroundColor =   messagess?.isFromCurrentUser == true  ? .green : .purple
        
        guard let messagess = messagess else {return}
         
        bubbleLeftAnchor =   bubble.leftAnchor.constraint(equalTo: leftAnchor,constant: 12)
        bubbleRightAnchor =  bubble.rightAnchor.constraint(equalTo: rightAnchor,constant: -12)
      
        if  messagess.fromId == Auth.auth().currentUser?.uid  {
        
            bubbleLeftAnchor.isActive = false
            bubbleRightAnchor.isActive = true
                
            } else {
                
                bubbleRightAnchor.isActive = false
                bubbleLeftAnchor.isActive = true
                
            }
            
    }
    
    
    
}

解决方法

每次调用configure()时,您都会创建 NEW 约束。

您需要移动这两行:

    bubbleLeftAnchor =   bubble.leftAnchor.constraint(equalTo: leftAnchor,constant: 12)
    bubbleRightAnchor =  bubble.rightAnchor.constraint(equalTo: rightAnchor,constant: -12)

configure()到您的init()函数:

override init(frame: CGRect) {
    super.init(frame: .zero)
    
    addSubview(bubble)
    bubble.addSubview(label)
    
    bubble.anchor(top: topAnchor,bottom: bottomAnchor,paddingTop: 10,paddingLeft: 50,paddingBottom: 20)
    bubble.widthAnchor.constraint(lessThanOrEqualToConstant: 250).isActive = true
    
    label.anchor(top: bubble.topAnchor,left: bubble.leftAnchor,bottom: bubble.bottomAnchor,right: bubble.rightAnchor,paddingTop: 4,paddingLeft: 4,paddingBottom: 4,paddingRight: 4)

    // create these constraints in init
    bubbleLeftAnchor =   bubble.leftAnchor.constraint(equalTo: leftAnchor,constant: -12)
    
}