1
votes

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?

1
Have you checked your debug output for unsatisfiable constraints? Note that if textViewDidChange: is called multiple times, it will keep adding new constraints without removing the previously added ones, which might cause some autolayout issuescrizzis
Yeah, one of my constraints is unsatisfiable. It's the new width constraint and it attempts to fix it by breaking the centerX constraintStefan
You are adding more constraints, not changing the old one. The old multiplier constraint is in conflict with the new one.Lou Franco

1 Answers

1
votes

For resizing:You can increase and decrease the size of the textview dynamically by not setting fixed constraints to super view and playing around with compression hugging and resistance priorities. For relocating: Enable and disable constraints at various places in textview delegate