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?