当 TextView 中有字符时,我想同时调整和重新定位自动布局约束。是的,我正在使用 textview 委托(delegate)。是的, View 在加载时会正确布局。当我尝试调整 View 大小时出现问题......我可以按照我想要的方式移动它(centerX),但是当我更改宽度属性时, View 动画消失并消失......
这是代码...
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
这是第二部分
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
})
}
会不会是因为我同时使用了 anchor 和 NSLayoutConstraints?
最佳答案
对于调整大小:您可以动态地增加和减少 TextView 的大小,方法是不为 super View 设置固定约束并尝试使用压缩拥抱和阻力优先级。 对于重定位:在 textview delegate 的不同位置启用和禁用约束
关于ios - 更改自动布局约束时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44315619/