Creo el problema es que necesitas desactivar los constraints anteriores antes de activar los nuevos. Al parecer tienes un UICollectionView
y supongo estás reusando las celdas en collectionView(_:cellForItemAt:)
, esto podría estar causando que ambos constraints (leading y trailing) se estén activando y por eso se alineen.
Podrías probar dentro de setupConstraints()
algo como:
func setupConstraints() {
...
let commentUserLeading = ...
let commentAnswerLeading = ...
let commentUserTrailling = ...
let commentAnswerTrailing = ...
if comments?.isUser == true {
commentAnswerLeading.isActive = false
commentAnswerTrailing.isActive = false
commentUserLeading.isActive = true
commentAnswerLeadingcommentUserTrailing.isActive = falsetrue
} else {
commentUserTraillingcommentUserLeading.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
*/
...
}