I want to both resize and relocate an auto layout constraint when there are characters in a textview. Yes, I am using a textview delegate. Yes, the view does layout properly when the view loads. The problem arises when I attempt to resize the view... I can move it how I want (centerX), however when I change the width property the view animates away and disappears...
Here's the code...
paddingView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.5).isActive = true
NSLayoutConstraint(item: paddingView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 0.75, constant: 0).isActive = true
paddingView.heightAnchor.constraint(equalToConstant: 6.0).isActive = true
bottomConstraint = paddingView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0.0)
bottomConstraint?.isActive = true
Here's the second part
func textViewDidChange(_ textView: UITextView) {
let currentString: String = textView.text!
let length: Int = (currentString.characters.count )
if length > 0 {
NSLayoutConstraint(item: paddingView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1, constant: 0).isActive = true
paddingView.widthAnchor.constraint(equalTo: view.widthAnchor, multiplier: 0.75).isActive = true
}else{
}
UIView.animate(withDuration: 0.2, delay: 0, options: UIViewAnimationOptions.curveEaseOut, animations: {
self.view.layoutIfNeeded()
}, completion: { (completed) in
})
}
Could it be because I am using anchors and NSLayoutConstraints together?
textViewDidChange:
is called multiple times, it will keep adding new constraints without removing the previously added ones, which might cause some autolayout issues – crizzis