Constraints in a chatView

0

I have a menu with three options, the last option is a very simple chat, when entering for the first time, the dialog balloons are well arranged, but when selecting another menu option and then returning to the chat, the balloons are aligned , check the photos so you can be clearer.

when entering the view for the first time:

and then the constraints are unsettled:

I already tried the storyboards but the same thing happens to me, also try a layoutifneeded, and the same, I do not put it in a viewWillApear because I believe it in a UIView, not in a ViewController, I also tried from the delegate "cellForItem "place a:

      cell.layoutIfNeeded()

even in the custom cell in the function:

       override func awakeFromNib() {
              super.awakeFromNib()
              self.layoutIfNeeded()
       }

and this is the code of the constrains ..

        var dialogViewLeading: NSLayoutConstraint?
        var dialogViewTrailing: NSLayoutConstraint? 

         func setupConstraints() {
    dialogView.translatesAutoresizingMaskIntoConstraints = false
    commentTextView.translatesAutoresizingMaskIntoConstraints = false

    dialogView.topAnchor.constraint(equalTo: self.topAnchor, constant: 0).isActive = true
    dialogView.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true

    let commentUserLeading = dialogView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 50)
    let commentAnswerLeading = dialogView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 30)
    let commentUserTrailling = dialogView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -30)
    let commentAnswerTrailing = dialogView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -50)
    dialogViewLeading = comments?.isUser == false ? commentAnswerLeading : commentUserLeading
    dialogViewTrailing = comments?.isUser == false ? commentAnswerTrailing : commentUserTrailling
    dialogViewLeading?.isActive = true
    dialogViewTrailing?.isActive = true

    commentTextView.centerXAnchor.constraint(equalTo: dialogView.centerXAnchor, constant: 0).isActive = true
    commentTextView.centerYAnchor.constraint(equalTo: dialogView.bottomAnchor, constant: 0).isActive = true
    commentTextView.widthAnchor.constraint(equalTo: dialogView.widthAnchor, multiplier: 1).isActive = true
    commentTextView.heightAnchor.constraint(equalTo: dialogView.heightAnchor, multiplier: 2).isActive = true
}

I pass the function for the constraints when setting the data to the model ...

       var comments: CommentsModel? {
    didSet {
        guard let comment = comments else { return }
        commentTextView.text = comment.description

        let colorCommentUser = #colorLiteral(red: 0.2224622369, green: 0.7220397592, blue: 0.8129917979, alpha: 1)
        let colorCommentAnswer = #colorLiteral(red: 0.9175571799, green: 0.9176927209, blue: 0.9175387025, alpha: 1)
        dialogView.backgroundColor = comment.isUser == false ? colorCommentAnswer : colorCommentUser
        commentTextView.textColor = comment.isUser == false ? #colorLiteral(red: 0.3332946301, green: 0.3333562315, blue: 0.333286047, alpha: 1) : #colorLiteral(red: 0.9175571799, green: 0.9176927209, blue: 0.9175387025, alpha: 1)
        setupConstraints()
    }
}

Does anyone have any idea what is going on? or what am I doing wrong?

    
asked by Yan Cervantes 26.11.2018 в 19:18
source

2 answers

0

On some help from Angel Teyez, what I did was to place a delegate from the CollectionView, didEndDisplaying and simply reload the function of activating the constraints:

    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
         guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: CommentsGetCollectionViewCell.identifier, for: indexPath) as? CommentsGetCollectionViewCell else { return }
         cell.ActivateConstraintsForComments()
}

As in cellForItem I create the cell and tell it which class it is ... and I simply call the function.

    
answered by 01.12.2018 / 22:12
source
0

I think the problem is that you need to deactivate the previous constraints before activating the new ones. You seem to have a UICollectionView and I assume you are reusing the cells in collectionView(_:cellForItemAt:) , this could be causing both constraints ( leading and trailing ) are being activated and that's why they line up.

You could try in setupConstraints() something like:

func setupConstraints() {
    ...
    let commentUserLeading = ...
    let commentAnswerLeading = ...
    let commentUserTrailling = ...
    let commentAnswerTrailing = ...
    if comments?.isUser == true {
        commentAnswerLeading.isActive = false
        commentAnswerTrailing.isActive = false
        commentUserLeading.isActive = true
        commentUserTrailing.isActive = true
    } else {
        commentUserLeading.isActive = false
        commentUserTrailing.isActive = false
        commentAnswerLeading.isActive = true
        commentAnswerTrailing.isActive = true
    }

    /* O incluso más resumido podría ser...
    commentUserLeading.isActive = comments?.isUser == true
    commentUserTrailing.isActive = comments?.isUser == true
    commentAnswerLeading.isActive = comments?.isUser == true
    commentAnswerTrailing.isActive = comments?.isUser == true
    */

    ...
}
    
answered by 27.11.2018 в 20:32